angular时间格式化

版权声明:转载请注明出处并附上原文链接,谢谢! https://blog.csdn.net/CODING_1/article/details/85268725

angular在格式化时间时不需要自己写方法,内置的管道可以直接使用
在组件中直接使用{{date | date:'yyyy-MM-dd HH:mm:ss'}}
在class中使用

  1. 引入 DatePipe import { DatePipe } from '@angular/common';
  2. 注入 constructor中,constructor(private datePipe: DatePipe) {}
  3. 调用transform方法
import { DatePipe } from '@angular/common';
constructor(
    private datePipe: DatePipe,
) {}
formatDate(date) {
    return this.datePipe.transform(date, 'yyyy-MM-dd');
}

猜你喜欢

转载自blog.csdn.net/CODING_1/article/details/85268725