Skip to content
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
12 changes: 11 additions & 1 deletion src/material-luxon-adapter/adapter/luxon-date-adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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\./,
Expand Down
9 changes: 5 additions & 4 deletions src/material-luxon-adapter/adapter/luxon-date-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,11 @@ export class LuxonDateAdapter extends DateAdapter<LuxonDateTime> {
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 {
Expand Down