Skip to content

Commit fbb53e2

Browse files
crisbetojosephperrott
authored andcommitted
fix(moment-date-adapter): not returning utc date when parsing (#12029)
1 parent 91e990f commit fbb53e2

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

src/material-moment-adapter/adapter/moment-date-adapter.spec.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -420,17 +420,33 @@ describe('MomentDateAdapter with MAT_MOMENT_DATE_ADAPTER_OPTIONS override', () =
420420
beforeEach(async(() => {
421421
TestBed.configureTestingModule({
422422
imports: [MomentDateModule],
423-
providers: [
424-
{ provide: MAT_MOMENT_DATE_ADAPTER_OPTIONS, useValue: { useUtc: true } }
425-
]
423+
providers: [{
424+
provide: MAT_MOMENT_DATE_ADAPTER_OPTIONS,
425+
useValue: {useUtc: true}
426+
}]
426427
}).compileComponents();
427428
}));
428429

429430
beforeEach(inject([DateAdapter], (d: MomentDateAdapter) => {
430431
adapter = d;
431432
}));
432433

433-
it('should create Moment date in utc format if option useUtc is set', () => {
434-
expect(adapter.createDate(2017, JAN, 5).isUTC()).toBeTruthy();
434+
describe('use UTC', () => {
435+
it('should create Moment date in UTC', () => {
436+
expect(adapter.createDate(2017, JAN, 5).isUtc()).toBe(true);
437+
});
438+
439+
it('should create today in UTC', () => {
440+
expect(adapter.today().isUtc()).toBe(true);
441+
});
442+
443+
it('should parse dates to UTC', () => {
444+
expect(adapter.parse('1/2/2017', 'MM/DD/YYYY')!.isUtc()).toBe(true);
445+
});
446+
447+
it('should return UTC date when deserializing', () => {
448+
expect(adapter.deserialize('1985-04-12T23:20:50.52Z')!.isUtc()).toBe(true);
449+
});
435450
});
451+
436452
});

src/material-moment-adapter/adapter/moment-date-adapter.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,7 @@ export class MomentDateAdapter extends DateAdapter<Moment> {
158158
throw Error(`Invalid date "${date}". Date has to be greater than 0.`);
159159
}
160160

161-
let result;
162-
if (this.options && this.options.useUtc) {
163-
result = moment.utc({ year, month, date }).locale(this.locale);
164-
} else {
165-
result = moment({ year, month, date }).locale(this.locale);
166-
}
161+
const result = this._createMoment({year, month, date}).locale(this.locale);
167162

168163
// If the result isn't valid, the date must have been out of bounds for this month.
169164
if (!result.isValid()) {
@@ -174,14 +169,14 @@ export class MomentDateAdapter extends DateAdapter<Moment> {
174169
}
175170

176171
today(): Moment {
177-
return moment().locale(this.locale);
172+
return this._createMoment().locale(this.locale);
178173
}
179174

180175
parse(value: any, parseFormat: string | string[]): Moment | null {
181176
if (value && typeof value == 'string') {
182-
return moment(value, parseFormat, this.locale);
177+
return this._createMoment(value, parseFormat, this.locale);
183178
}
184-
return value ? moment(value).locale(this.locale) : null;
179+
return value ? this._createMoment(value).locale(this.locale) : null;
185180
}
186181

187182
format(date: Moment, displayFormat: string): string {
@@ -216,13 +211,13 @@ export class MomentDateAdapter extends DateAdapter<Moment> {
216211
deserialize(value: any): Moment | null {
217212
let date;
218213
if (value instanceof Date) {
219-
date = moment(value);
214+
date = this._createMoment(value);
220215
}
221216
if (typeof value === 'string') {
222217
if (!value) {
223218
return null;
224219
}
225-
date = moment(value, moment.ISO_8601).locale(this.locale);
220+
date = this._createMoment(value, moment.ISO_8601).locale(this.locale);
226221
}
227222
if (date && this.isValid(date)) {
228223
return date;
@@ -241,4 +236,9 @@ export class MomentDateAdapter extends DateAdapter<Moment> {
241236
invalid(): Moment {
242237
return moment.invalid();
243238
}
239+
240+
/** Creates a Moment instance while respecting the current UTC settings. */
241+
private _createMoment(...args: any[]): Moment {
242+
return (this.options && this.options.useUtc) ? moment.utc(...args) : moment(...args);
243+
}
244244
}

0 commit comments

Comments
 (0)