diff --git a/npm/ng-packs/packages/theme-shared/src/lib/adapters/date-time.adapter.ts b/npm/ng-packs/packages/theme-shared/src/lib/adapters/date-time.adapter.ts index c461b9725b..813e2c20ff 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/adapters/date-time.adapter.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/adapters/date-time.adapter.ts @@ -6,11 +6,15 @@ export class DateTimeAdapter { value!: Partial; fromModel(value: string | Date): Partial | null { - if (!value) return null; + if (!value) { + return null; + } const date = new Date(value); - if (isNaN(date as unknown as number)) return null; + if (isNaN(date as unknown as number)) { + return null; + } this.value = { year: date.getFullYear(), @@ -25,12 +29,13 @@ export class DateTimeAdapter { } toModel(value: Partial | null): string { - if (!value) return ''; + if (!value) { + return ''; + } const now = new Date(); const newValue = { - // TODO look for strict mode errors year: now.getUTCFullYear(), month: now.getMonth() + 1, day: now.getDate(), @@ -42,15 +47,17 @@ export class DateTimeAdapter { } as NgbDateTimeStruct; const date = new Date( - newValue.year, - newValue.month - 1, - newValue.day, - newValue.hour, - newValue.minute, - newValue.second, + Date.UTC( + newValue.year, + newValue.month - 1, + newValue.day, + newValue.hour, + newValue.minute, + newValue.second, + ), ); - return new Date(date).toISOString(); + return date.toISOString().replace('Z', ''); } } diff --git a/npm/ng-packs/packages/theme-shared/src/lib/tests/date-time.adapter.spec.ts b/npm/ng-packs/packages/theme-shared/src/lib/tests/date-time.adapter.spec.ts index 2c00b28fae..d6074a90cd 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/tests/date-time.adapter.spec.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/tests/date-time.adapter.spec.ts @@ -1,4 +1,5 @@ - import { DateTimeAdapter } from '../adapters/date-time.adapter' +import { DateTimeAdapter } from '../adapters/date-time.adapter'; + describe('DateTime Adapter', () => { const adapter = new DateTimeAdapter(); const date = new Date(2002, 2, 30, 13, 30, 45, 0); @@ -9,6 +10,7 @@ describe('DateTime Adapter', () => { const minute = date.getMinutes(); const second = date.getSeconds(); + const isoWithoutZone = date.toISOString().replace('Z', ''); describe('#fromModel', () => { test.each` param | expected @@ -23,12 +25,12 @@ describe('DateTime Adapter', () => { describe('#toModel', () => { test.each` - param | expected - ${undefined} | ${''} - ${null} | ${''} - ${{ year, month, day, hour, minute, second }} | ${date.toISOString()} + param | expected + ${undefined} | ${''} + ${null} | ${''} + ${isoWithoutZone} | ${isoWithoutZone} `('should return $expected when $param is given', ({ param, expected }) => { - expect(adapter.toModel(param)).toEqual(expected); + expect(adapter.toModel(adapter.fromModel(param))).toEqual(expected); }); }); });