Skip to content
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
6 changes: 6 additions & 0 deletions packages/@internationalized/date/src/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ export function parseTime(value: string): Time {
export function parseDate(value: string): CalendarDate {
let m = value.match(DATE_RE);
if (!m) {
if (ABSOLUTE_RE.test(value)) {
throw new Error(`Invalid ISO 8601 date string: ${value}. Use parseAbsolute() instead.`);
}
throw new Error('Invalid ISO 8601 date string: ' + value);
}

Expand All @@ -63,6 +66,9 @@ export function parseDate(value: string): CalendarDate {
export function parseDateTime(value: string): CalendarDateTime {
let m = value.match(DATE_TIME_RE);
if (!m) {
if (ABSOLUTE_RE.test(value)) {
throw new Error(`Invalid ISO 8601 date time string: ${value}. Use parseAbsolute() instead.`);
}
throw new Error('Invalid ISO 8601 date time string: ' + value);
}

Expand Down
18 changes: 18 additions & 0 deletions packages/@internationalized/date/tests/string.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ describe('string conversion', function () {
expect(() => parseDate('2020-02-30')).toThrow();
expect(() => parseDate('2024-01-00')).toThrow();
});

it('should provide helpful error when passed an absolute datetime string', function () {
expect(() => parseDate('2023-10-07T12:34:56.789Z')).toThrow(/parseAbsolute/);
const isoString = new Date('2023-10-07T12:00:00Z').toISOString();
expect(() => parseDate(isoString)).toThrow(/parseAbsolute/);
expect(() => parseDate('2020-02-03T12:23:24Z')).toThrow(/parseAbsolute/);
expect(() => parseDate('2020-02-03T12:23:24+05:00')).toThrow(/parseAbsolute/);
expect(() => parseDate('2020-02-03T12:23:24-08:00')).toThrow(/parseAbsolute/);
});
});

describe('CalendarDate#toString', function () {
Expand Down Expand Up @@ -195,6 +204,15 @@ describe('string conversion', function () {
expect(() => parseDateTime('2020-02-03T23:99')).toThrow();
expect(() => parseDateTime('2020-02-03T12:22:99')).toThrow();
});

it('should provide helpful error when passed an absolute datetime string', function () {
expect(() => parseDateTime('2023-10-07T12:34:56.789Z')).toThrow(/parseAbsolute/);
const isoString = new Date('2023-10-07T12:00:00Z').toISOString();
expect(() => parseDateTime(isoString)).toThrow(/parseAbsolute/);
expect(() => parseDateTime('2020-02-03T12:23:24Z')).toThrow(/parseAbsolute/);
expect(() => parseDateTime('2020-02-03T12:23:24+05:00')).toThrow(/parseAbsolute/);
expect(() => parseDateTime('2020-02-03T12:23:24-08:00')).toThrow(/parseAbsolute/);
});
});

describe('CalendarDateTime#toString', function () {
Expand Down