|
8 | 8 |
|
9 | 9 | import {Inject, Injectable, Optional, LOCALE_ID} from '@angular/core';
|
10 | 10 | import {DateAdapter} from './date-adapter';
|
| 11 | +import {extendObject} from '../util/object-extend'; |
11 | 12 |
|
12 | 13 |
|
13 | 14 | // TODO(mmalerba): Remove when we no longer support safari 9.
|
@@ -56,6 +57,14 @@ export class NativeDateAdapter extends DateAdapter<Date> {
|
56 | 57 | super.setLocale(localeId);
|
57 | 58 | }
|
58 | 59 |
|
| 60 | + /** |
| 61 | + * Whether to use `timeZone: 'utc'` with `Intl.DateTimeFormat` when formatting dates. |
| 62 | + * Without this `Intl.DateTimeFormat` sometimes chooses the wrong timeZone, which can throw off |
| 63 | + * the result. (e.g. in the en-US locale `new Date(1800, 7, 14).toLocaleDateString()` |
| 64 | + * will produce `'8/13/1800'`. |
| 65 | + */ |
| 66 | + useUtcForDisplay = true; |
| 67 | + |
59 | 68 | getYear(date: Date): number {
|
60 | 69 | return date.getFullYear();
|
61 | 70 | }
|
@@ -154,6 +163,12 @@ export class NativeDateAdapter extends DateAdapter<Date> {
|
154 | 163 |
|
155 | 164 | format(date: Date, displayFormat: Object): string {
|
156 | 165 | if (SUPPORTS_INTL_API) {
|
| 166 | + if (this.useUtcForDisplay) { |
| 167 | + date = new Date(Date.UTC( |
| 168 | + date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), |
| 169 | + date.getMinutes(), date.getSeconds(), date.getMilliseconds())); |
| 170 | + displayFormat = extendObject({}, displayFormat, {timeZone: 'utc'}); |
| 171 | + } |
157 | 172 | let dtf = new Intl.DateTimeFormat(this.locale, displayFormat);
|
158 | 173 | return this._stripDirectionalityCharacters(dtf.format(date));
|
159 | 174 | }
|
|
0 commit comments