UTC date for mat-date picker in angular 6

 ̄綄美尐妖づ 提交于 2020-01-16 09:14:53

问题


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 file

var nowDate       =  new Date();
this.startdate    =  nowDate;
this.enddate      =  nowDate;

.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>

回答1:


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 } }
  ]
})

I have created a sample on stackblitz. You can find out more at Choosing a date implementation and date format settings.




回答2:


I created my own Date Adapter as the example above didn't work for me. This also formats the date to a long format.

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';
    }
  }
}


来源:https://stackoverflow.com/questions/54288803/utc-date-for-mat-date-picker-in-angular-6

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!