Angular 6 中 mat-datepicker 的 UTC 日期

San*_*iar 2 angular angular6

我已经用于mat-datepicker我的 Angular 6 项目。但是在日期选择器中显示当前时区日期。而不是这个,我需要显示当前的 UTC 日期。

这是我的代码

.ts 文件

var nowDate       =  new Date();
this.startdate    =  nowDate;
this.enddate      =  nowDate;
Run Code Online (Sandbox Code Playgroud)

.html 文件

<mat-form-field style="margin-right: 25px;">
                    <input matInput [matDatepicker]="picker_start" placeholder="Start Date" [(ngModel)]="startdate" [ngModelOptions]="{timezone: 'UTC'}">
                    <mat-datepicker-toggle matSuffix [for]="picker_start"></mat-datepicker-toggle>
                    <mat-datepicker #picker_start></mat-datepicker>
                  </mat-form-field>
Run Code Online (Sandbox Code Playgroud)

shh*_*men 14

您可以将 Moment.js 与 Material Datepicker 一起使用,并相应地设置选项,如下所示:

@NgModule({
  imports: [MatDatepickerModule, MatMomentDateModule],
  providers: [
    { provide: MAT_MOMENT_DATE_ADAPTER_OPTIONS, useValue: { useUtc: true } }
  ]
})
Run Code Online (Sandbox Code Playgroud)

我在stackblitz上创建了一个示例。您可以在选择日期实现和日期格式设置中找到更多信息。