Browse Source

Merge pull request #19221 from abpframework/issue-16904

Fix: Date filter does not work properly
pull/19250/head
oykuermann 2 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>;
fromModel(value: string | Date): Partial<NgbDateTimeStruct> | 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<NgbDateTimeStruct> | 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', '');
}
}

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', () => {
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);
});
});
});

Loading…
Cancel
Save