(Angular v.4.3.6を利用)
起動時にlocaleを設定 (JIT)
LOCALE_ID
を設定
@NgModule({ imports: [ BrowserModule, ], declarations: [ AppComponent ], providers: [ { provide: LOCALE_ID, useValue: navigator.language // useValue: 'ja' } ], bootstrap: [AppComponent] }) export class AppModule { }
動的にlocaleを変更
@Pipe({ name: 'date' }) export class DatePipeProxy implements PipeTransform { constructor(private localizationService: LocalizationService) { } public transform(value: any, pattern: string = 'mediumDate'): any { const ngPipe = new DatePipe(this.localizationService.locale); return ngPipe.transform(value, pattern); } }
タイムゾーンを変更
- 直接
Intl.DateTimeFormat
を利用するPipeをつくるDateTimeFormatOptions
のtimeZone
を設定- DatePipeの内部でも
DateTimeFormat
を利用しているが、現状だとtimeZone
を変更するAPIはない