要在Angular应用中使用日期时间格式,可以使用Angular的DatePipe。同时,如果您使用ng-bootstrap来处理日期选择器,您可以使用ng-bootstrap的NgbDateParserFormatter来格式化日期。
下面是一个使用Angular和ng-bootstrap的日期时间格式的示例:
npm install @ng-bootstrap/ng-bootstrap
import { NgModule } from '@angular/core';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
@NgModule({
imports: [NgbModule],
exports: [NgbModule]
})
export class YourModule { }
import { Component } from '@angular/core';
import { DatePipe } from '@angular/common';
import { NgbDateParserFormatter } from '@ng-bootstrap/ng-bootstrap';
constructor(private datePipe: DatePipe, private parserFormatter: NgbDateParserFormatter) { }
formatDate(date: Date): string {
return this.datePipe.transform(date, 'yyyy-MM-dd');
}
formatNgbDate(ngbDate: NgbDateStruct): string {
const date = this.parserFormatter.parse(ngbDate);
return this.datePipe.transform(date, 'yyyy-MM-dd');
}
{{ formatDate(myDate) }}
请注意,上述代码仅提供了一个示例,您可以根据您的需求进行修改。另外,您可能需要根据您的本地化设置调整日期格式字符串。
上一篇:angular / nest js scoket io中无法在所有客户端接收数据。
下一篇:Angular / ng2-charts: 在图表对象中获取JSON数据显示:无法读取未定义属性'length'