我刚接触过角度,并希望在组件ngOnInit方法中设置日期格式。我看到了一些使用管道运算符格式化数据的示例,但是我不知道如何在组件文件中格式化日期。
下面是代码
export class DashboardComponent implements OnInit {
formatdate = 'dd/MM/yyyy';
constructor(private auth: AuthService) { }
ngOnInit() {
console.log(new Date().toISOString())
}
}
您可以在此处找到有关日期管道的更多信息,例如格式。
如果要在组件中使用它,只需执行
pipe = new DatePipe('en-US'); // Use your own locale
现在,您可以简单地使用其transform方法,即
const now = Date.now();
const myFormattedDate = this.pipe.transform(now, 'short');
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句