Browse Source

Merge pull request #19221 from abpframework/issue-16904

Fix: Date filter does not work properly
pull/19250/head
oykuermann 3 years ago
committed by GitHub
parent
commit
f0b8e2cd39
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 29
      npm/ng-packs/packages/theme-shared/src/lib/adapters/date-time.adapter.ts
  2. 14
      npm/ng-packs/packages/theme-shared/src/lib/tests/date-time.adapter.spec.ts

29
npm/ng-packs/packages/theme-shared/src/lib/adapters/date-time.adapter.ts

@ -6,11 +6,15 @@ export class DateTimeAdapter {
value!: Partial<NgbDateTimeStruct>; value!: Partial<NgbDateTimeStruct>;
fromModel(value: string | Date): Partial<NgbDateTimeStruct> | null { fromModel(value: string | Date): Partial<NgbDateTimeStruct> | null {
if (!value) return null; if (!value) {
return null;
}
const date = new Date(value); 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 = { this.value = {
year: date.getFullYear(), year: date.getFullYear(),
@ -25,12 +29,13 @@ export class DateTimeAdapter {
} }
toModel(value: Partial<NgbDateTimeStruct> | null): string { toModel(value: Partial<NgbDateTimeStruct> | null): string {
if (!value) return ''; if (!value) {
return '';
}
const now = new Date(); const now = new Date();
const newValue = { const newValue = {
// TODO look for strict mode errors
year: now.getUTCFullYear(), year: now.getUTCFullYear(),
month: now.getMonth() + 1, month: now.getMonth() + 1,
day: now.getDate(), day: now.getDate(),
@ -42,15 +47,17 @@ export class DateTimeAdapter {
} as NgbDateTimeStruct; } as NgbDateTimeStruct;
const date = new Date( const date = new Date(
newValue.year, Date.UTC(
newValue.month - 1, newValue.year,
newValue.day, newValue.month - 1,
newValue.hour, newValue.day,
newValue.minute, newValue.hour,
newValue.second, newValue.minute,
newValue.second,
),
); );
return new Date(date).toISOString(); return date.toISOString().replace('Z', '');
} }
} }

14
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', () => { describe('DateTime Adapter', () => {
const adapter = new DateTimeAdapter(); const adapter = new DateTimeAdapter();
const date = new Date(2002, 2, 30, 13, 30, 45, 0); const date = new Date(2002, 2, 30, 13, 30, 45, 0);
@ -9,6 +10,7 @@ describe('DateTime Adapter', () => {
const minute = date.getMinutes(); const minute = date.getMinutes();
const second = date.getSeconds(); const second = date.getSeconds();
const isoWithoutZone = date.toISOString().replace('Z', '');
describe('#fromModel', () => { describe('#fromModel', () => {
test.each` test.each`
param | expected param | expected
@ -23,12 +25,12 @@ describe('DateTime Adapter', () => {
describe('#toModel', () => { describe('#toModel', () => {
test.each` test.each`
param | expected param | expected
${undefined} | ${''} ${undefined} | ${''}
${null} | ${''} ${null} | ${''}
${{ year, month, day, hour, minute, second }} | ${date.toISOString()} ${isoWithoutZone} | ${isoWithoutZone}
`('should return $expected when $param is given', ({ param, expected }) => { `('should return $expected when $param is given', ({ param, expected }) => {
expect(adapter.toModel(param)).toEqual(expected); expect(adapter.toModel(adapter.fromModel(param))).toEqual(expected);
}); });
}); });
}); });

Loading…
Cancel
Save