diff --git a/src/dateTime/__tests__/format.ts b/src/dateTime/__tests__/format.ts index b898151..a7fd91e 100644 --- a/src/dateTime/__tests__/format.ts +++ b/src/dateTime/__tests__/format.ts @@ -123,7 +123,7 @@ test('toISOString', () => { //invalid dates date = dateTimeUtc({input: '2017-12-32k'}); - expect(date.toISOString()).toBe(null); // 'An invalid date to iso string is null' + expect(() => date.toISOString()).toThrow(); // 'An invalid date to iso string is null' }); test('toISOString without UTC conversion', () => { @@ -143,7 +143,7 @@ test('toISOString without UTC conversion', () => { //invalid dates date = dateTimeUtc({input: '2017-12-32k'}).utcOffset('+05:30'); - expect(date.toISOString(true)).toBe(null); // 'An invalid date to iso string is null' + expect(() => date.toISOString(true)).toThrow(); // 'An invalid date to iso string is null' }); test('long years', () => { diff --git a/src/dateTime/dateTime.ts b/src/dateTime/dateTime.ts index 9a34d82..a15cc64 100644 --- a/src/dateTime/dateTime.ts +++ b/src/dateTime/dateTime.ts @@ -86,10 +86,7 @@ class DateTimeImpl implements DateTime { } toISOString(keepOffset?: boolean): string { - if (!this.isValid()) { - // @ts-expect-error it's not a correct value, but moment returns it with the same typings, luxon also returns null, dayjs throws an error. - return null; - } + // invalid date throws an error if (keepOffset) { return new Date(this.valueOf() + this.utcOffset() * 60 * 1000) .toISOString() @@ -139,6 +136,10 @@ class DateTimeImpl implements DateTime { return this._timeZone === 'system' ? guessUserTimeZone() : this._timeZone; } + if (!this.isValid()) { + return this; + } + const zone = normalizeTimeZone(timeZone, settings.getDefaultTimeZone()); let ts = this.valueOf(); let offset = timeZoneOffset(zone, ts); @@ -276,7 +277,7 @@ class DateTimeImpl implements DateTime { } valueOf(): number { - return this._timestamp; + return this.isValid() ? this._timestamp : NaN; } isSame(input?: DateTimeInput, granularity?: DurationUnit): boolean { @@ -378,7 +379,7 @@ class DateTimeImpl implements DateTime { } from(formaInput: DateTimeInput, withoutSuffix?: boolean): string { if (!this.isValid()) { - return INVALID_DATE_STRING; + return this._localeData.invalidDate || INVALID_DATE_STRING; } return fromTo(this, formaInput, this._localeData.relativeTime, withoutSuffix, true); } @@ -388,6 +389,9 @@ class DateTimeImpl implements DateTime { if (!locale) { return this._locale; } + if (!this.isValid()) { + return this; + } return createDateTime({ ts: this.valueOf(), timeZone: this._timeZone, @@ -399,13 +403,13 @@ class DateTimeImpl implements DateTime { return new Date(this.valueOf()); } unix(): number { - return Math.floor(this.valueOf() / 1000); + return this.isValid() ? Math.floor(this.valueOf() / 1000) : NaN; } utc(keepLocalTime?: boolean | undefined): DateTime { return this.timeZone(UtcTimeZone, keepLocalTime); } daysInMonth(): number { - return daysInMonth(this._c.year, this._c.month); + return this.isValid() ? daysInMonth(this._c.year, this._c.month) : NaN; } // eslint-disable-next-line complexity @@ -653,11 +657,13 @@ class DateTimeImpl implements DateTime { } toString(): string { - return this.isValid() ? this.toDate().toUTCString() : INVALID_DATE_STRING; + return this.isValid() + ? this.toDate().toUTCString() + : this._localeData.invalidDate || INVALID_DATE_STRING; } - toJSON(): string { - return this.toISOString(); + toJSON(): string | null { + return this.isValid() ? this.toISOString() : null; } /** diff --git a/src/typings/dateTime.ts b/src/typings/dateTime.ts index a609996..fd9fb7f 100644 --- a/src/typings/dateTime.ts +++ b/src/typings/dateTime.ts @@ -79,7 +79,7 @@ export interface DateTime { endOf(unitOfTime: StartOfUnit): DateTime; toDate(): Date; toISOString(keepOffset?: boolean): string; - toJSON(): string; + toJSON(): string | null; valueOf(): number; unix(): number; utc(keepLocalTime?: boolean): DateTime;