我为我的angular 6项目使用了mat-datepicker。但是在日期选择器中显示的是当前时区日期,而不是我需要显示当前的UTC日期。 这是我的代码
.ts文件
var nowDate = new Date();
this.startdate = nowDate;
this.enddate = nowDate;
.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>
答案 0 :(得分:0)
您可以将Moment.js与Material Datepicker一起使用,并相应地设置以下选项:
@NgModule({
imports: [MatDatepickerModule, MatMomentDateModule],
providers: [
{ provide: MAT_MOMENT_DATE_ADAPTER_OPTIONS, useValue: { useUtc: true } }
]
})
我在stackblitz上创建了一个示例。您可以在Choosing a date implementation and date format settings中找到更多信息。
答案 1 :(得分:0)
我创建了自己的日期适配器,因为上面的示例对我不起作用。这也将日期格式化为长格式。
import { NativeDateAdapter } from '@angular/material';
export class AppDateAdapter extends NativeDateAdapter {
format(date: Date, displayFormat: Object): string {
return this.formateDate(date);
}
deserialize(value: any): Date | null {
if ((typeof value === 'string') && (value.indexOf('-') > -1)) {
const str = value.split('-');
const dayArray = str[2].split('T');
const year = Number(str[0]);
const month = Number(str[1]) - 1;
const day = Number(dayArray[0]);
return new Date(Date.UTC(year, month, day));
}
const timestamp = typeof value === 'number' ? value : Date.parse(value);
return isNaN(timestamp) ? null : new Date(timestamp);
}
createDate(year: number, month: number, date: number): Date {
return new Date(Date.UTC(year, month, date));
}
parse(value: any): Date | null {
if ((typeof value === 'string') && (value.indexOf('-') > -1)) {
const str = value.split('-');
const dayArray = str[2].split('T');
const year = Number(str[0]);
const month = Number(str[1]) - 1;
const day = Number(dayArray[0]);
return new Date(year, month, day);
}
const timestamp = typeof value === 'number' ? value : Date.parse(value);
return isNaN(timestamp) ? null : new Date(timestamp);
}
formateDate(date: Date) {
const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
return date.getDate() + '' + this.nth(date.getDate()) + ' ' + months[date.getMonth()] + ' ' + date.getFullYear();
}
nth(d: number) {
if (d > 3 && d < 21) return 'th';
switch (d % 10) {
case 1: return 'st';
case 2: return 'nd';
case 3: return 'rd';
default: return 'th';
}
}
}
答案 2 :(得分:0)
提供此项以使用 UTC
{provide: MAT_MOMENT_DATE_ADAPTER_OPTIONS, useValue: { useUtc: true } },
提供这个用于本地支持(例如 FR)
{provide: MAT_DATE_LOCALE, useValue: 'fr-FR'},
使其工作的最重要的事情是使 DateAdapter 取决于您定义的设置。
{
provide: DateAdapter,
useClass: MomentDateAdapter,
deps: [MAT_DATE_LOCALE, MAT_MOMENT_DATE_ADAPTER_OPTIONS]
},
AppModule 的完整示例
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE } from '@angular/material/core';
import { MAT_MOMENT_DATE_ADAPTER_OPTIONS, MomentDateAdapter } from '@angular/material-moment-adapter';
@NgModule({
declarations: [
AppComponent,
],
imports: [
BrowserModule,
BrowserAnimationsModule,
],
providers: [
{provide: MAT_MOMENT_DATE_ADAPTER_OPTIONS, useValue: { useUtc: true } },
{provide: MAT_DATE_LOCALE, useValue: 'fr-FR'},
{
provide: DateAdapter,
useClass: MomentDateAdapter,
deps: [MAT_DATE_LOCALE, MAT_MOMENT_DATE_ADAPTER_OPTIONS]
},
],
bootstrap: [AppComponent]
})
export class AppModule {
}