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.

minnu merin alex
  • 87
  • 1
  • 2
  • 9

3 Answers3

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

sureshkumar
  • 184
  • 8
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

Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
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');
}
piet.t
  • 11,718
  • 21
  • 43
  • 52
HaSnen Tai
  • 1,353
  • 2
  • 14
  • 22