2

我有一系列日期,就像

      [
        "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"
      ]

如何使用 datepipe 将其转换为日期时间。

4

3 回答 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

于 2019-06-21T04:35:32.473 回答
0

你可以像这样使用管道

TS文件

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 文件

<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

于 2019-06-21T04:03:07.317 回答
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');
}
于 2019-06-21T04:05:02.223 回答