从代码中以特定格式日期显示日期?

Show date in specific format date from code?

我有一个 angular2 v4 应用程序,我必须以特定格式显示日期。

我的组件中有这个:

currentDate: Date = new Date();

在我的 HTML 我有这个。

<mat-form-field>
  <input matInput [(ngModel)]="currentReport.fromDate" type="text"/>
</mat-form-field>

但是在运行的时候,输入是这样的:

Sat Nov 17 2018 17:35:26 GMT-0600 (hora estándar central)

如何使用 ngModel 以 dd/mm/yyyy hh:mm 格式显示日期?

您需要将 Date 对象格式化为 string。一种方法是使用 moment .format (https://momentjs.com/docs/#/displaying/)

currentDate: Date = moment(new Date()).format('DD/MM/YYYY HH:mm')

如果这是你唯一需要的东西,那么在你的 ts 文件中像这样初始化它 currentDate: string = new Date().toLocaleString();

如果您还需要其他日期格式,请使用像 moment 这样的库