我有一个名为 Person 的界面,其中有姓名、姓氏、生日和其他变量。让我感到困惑的是 person.birthday 它发送的日期末尾带有一些字母。 我想要的是正常发送商店中的日期,但是当我想在 ngModel 中显示时只显示年、月、日期。 您可以在下面找到代码。
这是HTML中的代码
<app-input-field label="Gerbustag" labelWidth="105px">
<input class="inputBackground" type="date" [(ngModel)]="person.birthday">
</app-input-field>
这是人的界面。
export interface Person {
id: string;
firstName: string;
lastName: string;
birthday: Date;
我在TS里写的就是这样的。
birthday = new Date().toISOString().split('T')[0];
//This returns me good in the HTMl but this is not the correct birthday
你知道如何在 html 中仅显示此人的年-月-日
最佳答案
使用内置管道来获取日期的 Angular 。
{{ 字符串日期 | date :'hh:mm:ss' }} 将给出输出 09:44:12
{{ 字符串日期 | date :'dd-MMM-yyyy' }} 将给出输出 07-Nov-2016
{{ 字符串日期 | date :'h:mm' }} 将给出输出 9:44
{{strDate | date :'dd:MMM:yyyy hh-mm-ss z'}} 将给出输出 07:Nov:2016 09-44-12 India Standard Time
{{strDate | date :'hh:mm, E'}} 将给出输出 09:44, Mon
请参阅链接了解更多信息: https://www.concretepage.com/angular-2/angular-2-date-pipe-example
用法:
<input [ngModel]="person.birthday | date:'yyyy-MM-dd'" type="date" name="startDate"/>
关于javascript - 如何在 ngModel 日期中使用但仅使用月、年和日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50906737/