Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Date filter does not work properly #19221

Merged
merged 5 commits into from
Mar 8, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(),
Expand All @@ -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', '');
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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
Expand All @@ -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