角度日期选择器的UTC日期,角度6
问题描述:
我为我的angular 6项目使用了mat-datepicker.但是在日期选择器中显示的是当前时区日期,而不是我需要显示当前的UTC日期. 这是我的代码
i have used mat-datepicker for my angular 6 project.But in date picker in is showing current timezone date.Instead of this i need to display current UTC date. Here is my code
.ts文件
.ts file
var nowDate = new Date();
this.startdate = nowDate;
this.enddate = nowDate;
.html文件
.html file
<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>
答
您可以将Moment.js与Material Datepicker结合使用,并相应地设置选项,如下所示:
You can use Moment.js with Material Datepicker and set the options accordingly like below :
@NgModule({
imports: [MatDatepickerModule, MatMomentDateModule],
providers: [
{ provide: MAT_MOMENT_DATE_ADAPTER_OPTIONS, useValue: { useUtc: true } }
]
})
我已经在 stackblitz 上创建了一个示例.您可以在选择中找到更多信息.日期实现和日期格式设置.
I have created a sample on stackblitz. You can find out more at Choosing a date implementation and date format settings.