2

I have an array of dates and it would be like

      [
        "2019-06-17 09:21:20+05:30",
        "2019-06-18 09:21:20+05:30",
        "2019-06-19 09:21:20+05:30",
        "2019-06-20 09:21:20+05:30"
      ]

how to convert this to date time using datepipe.

3 Answers 3

3

import the DatePipe to your app.module file import { DatePipe} from '@angular/common'

and format your date objects using this Pre-defined format options

and include this code to your app.component.html page

{{ DateObjectValue | date:'medium'}}

see the attached stackblitz example link : FormatDateExample

Sign up to request clarification or add additional context in comments.

1 Comment

i am doing this for chart purpose.so can this be converted in typescript itself.thats what i need.
0

You can use pipe like this

TS file

export class AppComponent  {
  yourdate =   [
        "2019-06-17 09:21:20+05:30",
        "2019-06-18 09:21:20+05:30",
        "2019-06-19 09:21:20+05:30",
        "2019-06-20 09:21:20+05:30"
      ];
}

HTML file

<div *ngFor="let item of yourdate">
  {{item | date:'yyyy-MM-dd hh:mm'}}
</div>

https://stackblitz.com/edit/angular-date-pipe-array?file=src/app/app.component.html

Comments

0

StackBliz is here https://stackblitz.com/edit/angular-bvpg4r

1) Import DatePipe:

import { DatePipe } from '@angular/common';

2) Include DatePipe in your module's providers:

NgModule({
  providers: [DatePipe]
})
export class AppModule {
}
or component's providers:

@Component({
  selector: 'home',
  styleUrls: ['./home.component.css'],
  templateUrl: './home.component.html',
  providers: [DatePipe]
})
export class HomeComponent {
...

3) Inject it into your component's constructor like any other service:

constructor(private datePipe: DatePipe) {
}

4) Use it:

ngOnInit() {
     data=this.datePipe.transform("2019-06-20 09:21:20+05:30", 'yyyy-dd-MM');
}

4 Comments

i need in typescript..to dispaly in chart.i was using datepipe.so how to use datepipe for array of dates.
@minnu Please Check the Edited code Hope this is what you are looking for
i have an array of dates..do i need to loop the array ?or any other way
Yes of course loop the array and push your converted date into new array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.