diff --git a/src/material-luxon-adapter/adapter/luxon-date-adapter.spec.ts b/src/material-luxon-adapter/adapter/luxon-date-adapter.spec.ts index 306b5c7aed59..8ee5312d04cf 100644 --- a/src/material-luxon-adapter/adapter/luxon-date-adapter.spec.ts +++ b/src/material-luxon-adapter/adapter/luxon-date-adapter.spec.ts @@ -9,7 +9,7 @@ import {LOCALE_ID} from '@angular/core'; import {TestBed, waitForAsync} from '@angular/core/testing'; import {DateAdapter, MAT_DATE_LOCALE} from '@angular/material/core'; -import {DateTime} from 'luxon'; +import {DateTime, FixedOffsetZone, Settings} from 'luxon'; import {LuxonDateModule} from './index'; import {MAT_LUXON_DATE_ADAPTER_OPTIONS} from './luxon-date-adapter'; @@ -351,6 +351,16 @@ describe('LuxonDateAdapter', () => { expect(date).toEqual('2. jan. 2017'); }); + it('should format with a different timezone', () => { + Settings.defaultZone = FixedOffsetZone.parseSpecifier('UTC-12'); + + let date = adapter.format(DateTime.local(2017, JAN, 2, {zone: 'UTC-12'}), 'DD'); + expect(date).toEqual('Jan 2, 2017'); + + date = adapter.format(DateTime.local(2017, JAN, 2, {zone: 'UTC+12'}), 'DD'); + expect(date).toEqual('Jan 2, 2017'); + }); + it('should throw when attempting to format invalid date', () => { expect(() => adapter.format(DateTime.fromMillis(NaN), 'LL/dd/yyyy')).toThrowError( /LuxonDateAdapter: Cannot format invalid date\./, diff --git a/src/material-luxon-adapter/adapter/luxon-date-adapter.ts b/src/material-luxon-adapter/adapter/luxon-date-adapter.ts index f1a67d916394..663859c3aac1 100644 --- a/src/material-luxon-adapter/adapter/luxon-date-adapter.ts +++ b/src/material-luxon-adapter/adapter/luxon-date-adapter.ts @@ -193,10 +193,11 @@ export class LuxonDateAdapter extends DateAdapter { if (!this.isValid(date)) { throw Error('LuxonDateAdapter: Cannot format invalid date.'); } - return date - .setLocale(this.locale) - .setZone(this._useUTC ? 'utc' : undefined) - .toFormat(displayFormat); + if (this._useUTC) { + return date.setLocale(this.locale).setZone('utc').toFormat(displayFormat); + } else { + return date.setLocale(this.locale).toFormat(displayFormat); + } } addCalendarYears(date: LuxonDateTime, years: number): LuxonDateTime {