简体   繁体   English

如何为角形材料日期选择器设置特定的时区?

[英]How can I set up a specific timezone for angular material datepicker?

I am developing an application in Angular 7. There is a date field where I use angular material datepicker inout. 我正在Angular 7中开发应用程序。在日期字段中,我使用了角材料datepicker inout。 The problem is, datepicker always takes user local timezone when a date is selected. 问题是,选择日期后,日期选择器始终采用用户本地时区。 But I want to set it up to a specific timezone such as Eastern or Pacific timezone. 但我想将其设置为特定的时区,例如东部或太平洋时区。 When user will select a date, it should always come to that specific timezone ex. 当用户选择一个日期时,它应该总是到那个特定的时区。 06/22/2019 23:59 Pacific. 太平洋06/22/2019 23:59 I am new to angular. 我是新手。 How can do it in Angular 7 application? 如何在Angular 7应用程序中做到这一点? Thank you in advance. 先感谢您。

From the Angular Material docs , the default for the provided DateAdapter is to use the user's time zone. 从Angular Material文档中 ,提供的DateAdapter的默认值是使用用户的时区。

You'll have to provide your own DateAdapter to override this. 您必须提供自己的DateAdapter才能覆盖它。

I found this StackBlitz example that shows a possible way to solve this, but have not tried it. 我找到了这个StackBlitz示例该示例显示了解决此问题的可能方法,但还没有尝试过。

Here is a snippet from there: 这是一个摘录:

@Injectable()
export class CustomDateAdapter extends MomentDateAdapter {

  constructor( @Optional() @Inject(MAT_DATE_LOCALE) dateLocale: string) {
    super(dateLocale);
  }

  parse(value, parseFormat) {
    if (value && typeof value == 'string') {
      console.log(moment(value, parseFormat, this.locale, true));
      return moment(value, parseFormat, this.locale, true);
    }
    return value ? moment(value).locale(this.locale) : undefined;
  }
}

And providing it (simple version): 并提供它(简单版本):

{ provide: DateAdapter, useClass: CustomDateAdapter }

You'll need to the following: 您需要执行以下操作:

  1. Create a user date format pipe. 创建一个用户日期格式管道。
  2. Create a custom date adapter that extends NativeDateAdapter. 创建一个扩展NativeDateAdapter的自定义日期适配器。
  3. Import CustomDateAdapter into a module where it'll be used as useClass: CustomDateAdapter CustomDateAdapter导入模块中,用作useClass: CustomDateAdapter

app.module.ts app.module.ts

@NgModule({
  imports: [],
  declarations: [],
  exports: [],
  entryComponents: [],
  providers: [{
    provide: DateAdapter,
    useClass: CustomDateAdapter
  }]
})
export class AppModule {}

custom-date-adaptor.ts custom-date-adaptor.ts

export class CustomDateAdapter extends NativeDateAdapter {
  format(date: Date): string {
    const pipe = new UserDateFormatPipe();
    return pipe.transform(date);
  }
}

user-date-format.ts 用户日期格式

// using moment.js
export class UserDateFormatPipe implements PipeTransform {

  transform(date: string | Date, timeFormat: string = ''): string {
    const defaultValues = {
      dateFormat: 'MM-dd-yyyy',
      language: 'en-US',
      timeZone: 'Central' //change defaults accordingly
    };
    const timeZoneOffset = moment(new Date(date))
      .tz(defaultValues)
      .format('Z');
    const datePipe = new DatePipe(defaultValues.language);
    const dateFormat = timeFormat ? `${defaultValues.dateFormat}  ${timeFormat}` : defaultValues.dateFormat;
    return datePipe.transform(date, dateFormat, timeZoneOffset, defaultValues.language);
  }
}

Stackblitz-Demo Stackblitz-Demo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 材料日期选择器如何将时区设置为 UTC? - Angular material datepicker how to set timezone to UTC? Angular 8:如何设置 angular 材料日期选择器的时间? - Angular 8: How can I set time for angular material datepicker? 具有特定时区的材料日期选择器 - Material datepicker with specific timezone 如何从 Angular 2 为 DatePicker 设置时区? - How to set timeZone for DatePicker from Angular 2? 如何更改角形材料日期选择器中的日期格式? - How can I change date format in angular material datepicker? 如何向 Angular Material datepicker 的所有实例添加指令? - How can I add a directive to all instances of Angular Material datepicker? Datepicker Material Angular:如何显示月份的全名? - Datepicker Material Angular: How can i display full name of the month? 如何根据其他两个 Datepicker 字段在 Datepicker 字段上设置 Min 和 Max 值 - Angular Material - How do I set Min and Max values on a Datepicker field based on two other Datepicker fields - Angular Material 如何基于另一个 Datepicker 从 Angular 材料 Datepicker 验证日期 - How can I validate the Date from a Angular material Datepicker based on another Datepicker 如何使用Angular Material Datepicker禁用特定日期? - How to disable specific dates using Angular Material Datepicker?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM