如何使用datepipe在angular中格式化日期数组?

时间:2019-06-21 03:55:44

标签: typescript angular7 date-pipe

我有一个日期数组,就像

true

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

3 个答案:

答案 0 :(得分:3)

将DatePipe导入到您的app.module文件import { DatePipe} from '@angular/common'

并使用此Pre-defined format options

格式化日期对象

并将此代码添加到您的app.component.html页面

{{ DateObjectValue | date:'medium'}}

请参阅随附的stackblitz示例链接:FormatDateExample

答案 1 :(得分: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

答案 2 :(得分:0)

StackBliz在这里 https://stackblitz.com/edit/angular-bvpg4r

1)导入DatePipe:

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

2)在模块的提供程序中包括DatePipe:

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)像其他服务一样将其注入到组件的构造函数中:

constructor(private datePipe: DatePipe) {
}

4)使用它:

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