refactor(datetime): always use 2 digits for day and month formatting

Fixes #744
This commit is contained in:
Nicolas Meienberger 2026-04-04 17:19:53 +02:00
parent 1cb1a66ef1
commit 43d9cb837f
2 changed files with 10 additions and 10 deletions

View file

@ -64,13 +64,13 @@ describe("datetime formatters", () => {
});
test("formats calendar values with an explicit locale and timezone", () => {
expect(formatShortDateTime(sampleDate, { locale: "en-US", timeZone: "UTC" })).toBe("1/10, 2:30 PM");
expect(formatShortDateTime(sampleDate, { locale: "en-US", timeZone: "UTC" })).toBe("01/10, 2:30 PM");
});
test.each([
["MM/DD/YYYY", "1/10/2026"],
["DD/MM/YYYY", "10/1/2026"],
["YYYY/MM/DD", "2026/1/10"],
["MM/DD/YYYY", "01/10/2026"],
["DD/MM/YYYY", "10/01/2026"],
["YYYY/MM/DD", "2026/01/10"],
] as const)("formats numeric dates with %s order", (dateFormat, expected) => {
expect(formatDate(sampleDate, { locale: "en-US", timeZone: "UTC", dateFormat })).toBe(expected);
});
@ -106,6 +106,6 @@ describe("datetime formatters", () => {
dateFormat: "DD/MM/YYYY",
timeFormat: "24h",
}),
).toBe("10/1/2026, 14:30");
).toBe("10/01/2026, 14:30");
});
});

View file

@ -67,8 +67,8 @@ function formatConfiguredDate(date: Date, options: DateFormatOptions, includeYea
const dateFormat = options.dateFormat ?? DEFAULT_DATE_FORMAT;
const safeDateFormat = DATE_FORMATS.includes(dateFormat) ? dateFormat : DEFAULT_DATE_FORMAT;
const parts = getDateTimeFormat(options.locale, options.timeZone, {
month: "numeric",
day: "numeric",
month: "2-digit",
day: "2-digit",
year: "numeric",
}).formatToParts(date);
const values = {
@ -148,7 +148,7 @@ export function inferDateTimePreferences(locale?: string) {
};
}
// 1/10/2026, 2:30 PM
// 01/10/2026, 2:30 PM
function formatDateTime(date: DateInput, options: DateFormatOptions = {}): string {
return formatValidDate(
date,
@ -161,7 +161,7 @@ function formatDateWithMonth(date: DateInput, options: DateFormatOptions = {}):
return formatValidDate(date, (validDate) => formatConfiguredDateWithMonth(validDate, options));
}
// 1/10/2026
// 01/10/2026
function formatDate(date: DateInput, options: DateFormatOptions = {}): string {
return formatValidDate(date, (validDate) => formatConfiguredDate(validDate, options, true));
}
@ -171,7 +171,7 @@ function formatShortDate(date: DateInput, options: DateFormatOptions = {}): stri
return formatValidDate(date, (validDate) => formatConfiguredDate(validDate, options, false));
}
// 1/10, 2:30 PM
// 01/10, 2:30 PM
function formatShortDateTime(date: DateInput, options: DateFormatOptions = {}): string {
return formatValidDate(
date,