Skip to content

fix(moment-date-adapter): not returning utc date when parsing #12029

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions src/material-moment-adapter/adapter/moment-date-adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,17 +420,33 @@ describe('MomentDateAdapter with MAT_MOMENT_DATE_ADAPTER_OPTIONS override', () =
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [MomentDateModule],
providers: [
{ provide: MAT_MOMENT_DATE_ADAPTER_OPTIONS, useValue: { useUtc: true } }
]
providers: [{
provide: MAT_MOMENT_DATE_ADAPTER_OPTIONS,
useValue: {useUtc: true}
}]
}).compileComponents();
}));

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

it('should create Moment date in utc format if option useUtc is set', () => {
expect(adapter.createDate(2017, JAN, 5).isUTC()).toBeTruthy();
describe('use UTC', () => {
it('should create Moment date in UTC', () => {
expect(adapter.createDate(2017, JAN, 5).isUtc()).toBe(true);
});

it('should create today in UTC', () => {
expect(adapter.today().isUtc()).toBe(true);
});

it('should parse dates to UTC', () => {
expect(adapter.parse('1/2/2017', 'MM/DD/YYYY')!.isUtc()).toBe(true);
});

it('should return UTC date when deserializing', () => {
expect(adapter.deserialize('1985-04-12T23:20:50.52Z')!.isUtc()).toBe(true);
});
});

});
22 changes: 11 additions & 11 deletions src/material-moment-adapter/adapter/moment-date-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,7 @@ export class MomentDateAdapter extends DateAdapter<Moment> {
throw Error(`Invalid date "${date}". Date has to be greater than 0.`);
}

let result;
if (this.options && this.options.useUtc) {
result = moment.utc({ year, month, date }).locale(this.locale);
} else {
result = moment({ year, month, date }).locale(this.locale);
}
const result = this._createMoment({year, month, date}).locale(this.locale);

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

today(): Moment {
return moment().locale(this.locale);
return this._createMoment().locale(this.locale);
}

parse(value: any, parseFormat: string | string[]): Moment | null {
if (value && typeof value == 'string') {
return moment(value, parseFormat, this.locale);
return this._createMoment(value, parseFormat, this.locale);
}
return value ? moment(value).locale(this.locale) : null;
return value ? this._createMoment(value).locale(this.locale) : null;
}

format(date: Moment, displayFormat: string): string {
Expand Down Expand Up @@ -216,13 +211,13 @@ export class MomentDateAdapter extends DateAdapter<Moment> {
deserialize(value: any): Moment | null {
let date;
if (value instanceof Date) {
date = moment(value);
date = this._createMoment(value);
}
if (typeof value === 'string') {
if (!value) {
return null;
}
date = moment(value, moment.ISO_8601).locale(this.locale);
date = this._createMoment(value, moment.ISO_8601).locale(this.locale);
}
if (date && this.isValid(date)) {
return date;
Expand All @@ -241,4 +236,9 @@ export class MomentDateAdapter extends DateAdapter<Moment> {
invalid(): Moment {
return moment.invalid();
}

/** Creates a Moment instance while respecting the current UTC settings. */
private _createMoment(...args: any[]): Moment {
return (this.options && this.options.useUtc) ? moment.utc(...args) : moment(...args);
}
}