Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(core): keep years 1-99 as given in Calendar date helpers (#7465)

Harjoth Khara1c071c466929
diff --git a/.changeset/plain-date-years-1-99.md b/.changeset/plain-date-years-1-99.md
new file mode 100644
index 0000000000..a53cb5f25e
--- /dev/null
+++ b/.changeset/plain-date-years-1-99.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] `Calendar`: dates in years 1-99 now show their own year instead of 1900-1999. A value of `0001-01-15` was captioned "January 1901" with 1901's weekday layout and day labels, while `onChange` still emitted the year-1 ISO date. The shared date helpers behind the caption, day grid, week numbers, day counts, and timezone instants now keep years 1-99 as given. One limitation remains: in a timezone west of UTC, the instant for January 1 of year 1 still comes out a year early, because its timezone offset is read from a 1 BC wall clock without the era.
+
+@harjothkhara
diff --git a/packages/core/src/Calendar/Calendar.test.tsx b/packages/core/src/Calendar/Calendar.test.tsx
index 80b7cfe798..fc1bf3fda2 100644
--- a/packages/core/src/Calendar/Calendar.test.tsx
+++ b/packages/core/src/Calendar/Calendar.test.tsx
@@ -219,6 +219,31 @@ describe('Calendar', () => {
expect(handleChange).toHaveBeenCalledWith('2026-01-15', expect.any(Date));
});
+ it('shows years 1-99 as themselves, not as 1900-1999', async () => {
+ const user = userEvent.setup();
+ const handleChange = vi.fn();
+
+ render(
+ <Calendar
+ value="0001-01-15"
+ onChange={handleChange}
+ focusDate="0001-01-01"
+ />,
+ );
+
+ expect(screen.getByRole('grid', {name: 'January 1'})).toBeInTheDocument();
+ const day15 = getDayButton(15, 'January', 1);
+ expect(day15).toHaveAccessibleName(/^Monday, January 15, 1\b/);
+ expect(day15.closest('[role="gridcell"]')).toHaveAttribute(
+ 'aria-selected',
+ 'true',
+ );
+
+ await user.click(getDayButton(20, 'January', 1));
+ expect(handleChange).toHaveBeenCalledWith('0001-01-20', expect.any(Date));
+ expect(handleChange.mock.calls[0][1].getFullYear()).toBe(1);
+ });
+
it('keeps null single selection empty until the parent accepts a date', async () => {
const user = userEvent.setup();
const onChange = vi.fn();
diff --git a/packages/core/src/utils/plainDate.test.ts b/packages/core/src/utils/plainDate.test.ts
index 7677b475f4..6509f2aac2 100644
--- a/packages/core/src/utils/plainDate.test.ts
+++ b/packages/core/src/utils/plainDate.test.ts
@@ -149,6 +149,14 @@ describe('plainDateToDate / plainDateFromDate', () => {
expect(d.getDate()).toBe(15);
expect(plainDateFromDate(d)).toEqual(pd);
});
+
+ it('keeps years 1-99 instead of mapping them to 1900-1999', () => {
+ const pd: PlainDate = {year: 1, month: 1, day: 15};
+ const d = plainDateToDate(pd);
+ expect(d.getFullYear()).toBe(1);
+ expect(d.getDay()).toBe(1); // 0001-01-15 is a Monday
+ expect(plainDateFromDate(d)).toEqual(pd);
+ });
});
describe('plainDateToday', () => {
@@ -361,6 +369,15 @@ describe('plainDateDiffDays', () => {
),
).toBe(2);
});
+
+ it('counts across the year 99 to 100 boundary', () => {
+ expect(
+ plainDateDiffDays(
+ {year: 99, month: 12, day: 31},
+ {year: 100, month: 1, day: 1},
+ ),
+ ).toBe(1);
+ });
});
describe('plainDateToInstant / plainDateFromInstant', () => {
@@ -393,6 +410,20 @@ describe('plainDateToInstant / plainDateFromInstant', () => {
),
).toBe(Date.UTC(2026, 4, 13, 16, 30));
});
+
+ it('keeps years 1-99 instead of mapping them to 1900-1999', () => {
+ const date: PlainDate = {year: 1, month: 1, day: 15};
+ // 0001-01-01T00:00:00Z is -62135596800000
+ expect(plainDateToInstant(date, 'UTC', 9, 30)).toBe(
+ -62135596800000 + (14 * 24 + 9) * 3600000 + 30 * 60000,
+ );
+ expect(
+ plainDateFromInstant(
+ plainDateToInstant(date, 'America/Los_Angeles'),
+ 'America/Los_Angeles',
+ ),
+ ).toEqual(date);
+ });
});
describe('plainDateIsBefore / plainDateIsAfter', () => {
@@ -579,6 +610,11 @@ describe('plainDateGetWeekNumber', () => {
it('returns week 1 for Jan 4 (always in ISO week 1)', () => {
expect(plainDateGetWeekNumber({year: 2026, month: 1, day: 4})).toBe(1);
});
+
+ it('keeps years 1-99 instead of mapping them to 1900-1999', () => {
+ // 0004-01-01 is a Thursday (ISO week 1); 1904-01-01 was a Friday.
+ expect(plainDateGetWeekNumber({year: 4, month: 1, day: 1})).toBe(1);
+ });
});
describe('plainDateFormat', () => {
diff --git a/packages/core/src/utils/plainDate.ts b/packages/core/src/utils/plainDate.ts
index a4d34d6a4d..bcd809e9b3 100644
--- a/packages/core/src/utils/plainDate.ts
+++ b/packages/core/src/utils/plainDate.ts
@@ -45,7 +45,11 @@ export function plainDateToISO(pd: PlainDate): ISODateString {
}
export function plainDateToDate(pd: PlainDate): Date {
- return new Date(pd.year, pd.month - 1, pd.day);
+ // Unlike the Date constructor, setFullYear keeps years 0-99 as given
+ // instead of reading them as 1900-1999.
+ const d = new Date(0, 0, 1);
+ d.setFullYear(pd.year, pd.month - 1, pd.day);
+ return d;
}
export function plainDateFromDate(d: Date): PlainDate {
@@ -89,6 +93,24 @@ export function plainDateAddDays(pd: PlainDate, n: number): PlainDate {
return plainDateFromDate(d);
}
+/**
+ * `Date.UTC` with years 0-99 kept as given; `Date.UTC` itself reads them as
+ * 1900-1999.
+ */
+function utcTime(
+ year: number,
+ monthIndex: number,
+ day: number,
+ hour = 0,
+ minute = 0,
+ second = 0,
+): number {
+ const d = new Date(0);
+ d.setUTCFullYear(year, monthIndex, day);
+ d.setUTCHours(hour, minute, second);
+ return d.getTime();
+}
+
/**
* Whole calendar days from `a` to `b`, ignoring time of day. Positive when
* `b` is after `a`, negative when before. Uses UTC midnight so DST shifts
@@ -96,8 +118,8 @@ export function plainDateAddDays(pd: PlainDate, n: number): PlainDate {
*/
export function plainDateDiffDays(a: PlainDate, b: PlainDate): number {
const msPerDay = 24 * 60 * 60 * 1000;
- const aUTC = Date.UTC(a.year, a.month - 1, a.day);
- const bUTC = Date.UTC(b.year, b.month - 1, b.day);
+ const aUTC = utcTime(a.year, a.month - 1, a.day);
+ const bUTC = utcTime(b.year, b.month - 1, b.day);
return Math.round((bUTC - aUTC) / msPerDay);
}
@@ -152,7 +174,7 @@ export function getTimeZoneParts(
function getTimezoneOffsetMS(timezoneID: string, instant: number): number {
const parts = getTimeZoneParts(instant, timezoneID);
return (
- Date.UTC(
+ utcTime(
parts.year,
parts.month - 1,
parts.day,
@@ -169,7 +191,7 @@ export function plainDateToInstant(
hour = 0,
minute = 0,
): number {
- const utcGuess = Date.UTC(date.year, date.month - 1, date.day, hour, minute);
+ const utcGuess = utcTime(date.year, date.month - 1, date.day, hour, minute);
const firstOffset = getTimezoneOffsetMS(timezoneID, utcGuess);
const firstInstant = utcGuess - firstOffset;
const secondOffset = getTimezoneOffsetMS(timezoneID, firstInstant);
@@ -245,7 +267,7 @@ export function plainDateGetWeekNumber(pd: PlainDate): number {
const d = plainDateToDate(pd);
const dayNum = d.getDay() || 7;
d.setDate(d.getDate() + 4 - dayNum);
- const yearStart = new Date(d.getFullYear(), 0, 1);
+ const yearStart = plainDateToDate({year: d.getFullYear(), month: 1, day: 1});
return Math.ceil(((d.getTime() - yearStart.getTime()) / 86400000 + 1) / 7);
}