使用日期管道更改mat-date-picker的日期格式

时间:2020-04-25 18:41:27

标签: angularjs angular-material momentjs

我试图将角形材料日期选择器中的选择值更改为YYYY-MM-DD格式,如下所示:

  1. 在该字段的dateInput事件中,我调用了一个函数,该函数将使用日期管道更改日期格式并将值设置回。但是,格式不会改变。

HTML代码

<input matInput placeholder="Stop Date" [matDatepicker]="stopDate" (dateInput)="OnStopDate($event.value)" formControlName="stopDate">
<mat-datepicker-toggle matSuffix [for]="stopDate"></mat-datepicker-toggle>
<mat-datepicker #stopDate></mat-datepicker>`

TS代码

`OnStopDate(Value){
   const update= this.datepipe.transform(Value, 'YYYY-MM-dd');  //this value is being update
   this.myForm.controls.stopDate.setValue(update,{emitEvent: false});    // the value is not being set in yyyy-mm-dd but in mm-dd-yyyy
 }

我确实看到使用MomentDateAdapter作为here提供的解决方案。这是唯一的解决方法,为什么?

0 个答案:

没有答案