refactor(datetime): always use 2 digits for day and month formatting
Fixes #744
This commit is contained in:
parent
1cb1a66ef1
commit
43d9cb837f
2 changed files with 10 additions and 10 deletions
|
|
@ -64,13 +64,13 @@ describe("datetime formatters", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
test("formats calendar values with an explicit locale and timezone", () => {
|
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([
|
test.each([
|
||||||
["MM/DD/YYYY", "1/10/2026"],
|
["MM/DD/YYYY", "01/10/2026"],
|
||||||
["DD/MM/YYYY", "10/1/2026"],
|
["DD/MM/YYYY", "10/01/2026"],
|
||||||
["YYYY/MM/DD", "2026/1/10"],
|
["YYYY/MM/DD", "2026/01/10"],
|
||||||
] as const)("formats numeric dates with %s order", (dateFormat, expected) => {
|
] as const)("formats numeric dates with %s order", (dateFormat, expected) => {
|
||||||
expect(formatDate(sampleDate, { locale: "en-US", timeZone: "UTC", dateFormat })).toBe(expected);
|
expect(formatDate(sampleDate, { locale: "en-US", timeZone: "UTC", dateFormat })).toBe(expected);
|
||||||
});
|
});
|
||||||
|
|
@ -106,6 +106,6 @@ describe("datetime formatters", () => {
|
||||||
dateFormat: "DD/MM/YYYY",
|
dateFormat: "DD/MM/YYYY",
|
||||||
timeFormat: "24h",
|
timeFormat: "24h",
|
||||||
}),
|
}),
|
||||||
).toBe("10/1/2026, 14:30");
|
).toBe("10/01/2026, 14:30");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -67,8 +67,8 @@ function formatConfiguredDate(date: Date, options: DateFormatOptions, includeYea
|
||||||
const dateFormat = options.dateFormat ?? DEFAULT_DATE_FORMAT;
|
const dateFormat = options.dateFormat ?? DEFAULT_DATE_FORMAT;
|
||||||
const safeDateFormat = DATE_FORMATS.includes(dateFormat) ? dateFormat : DEFAULT_DATE_FORMAT;
|
const safeDateFormat = DATE_FORMATS.includes(dateFormat) ? dateFormat : DEFAULT_DATE_FORMAT;
|
||||||
const parts = getDateTimeFormat(options.locale, options.timeZone, {
|
const parts = getDateTimeFormat(options.locale, options.timeZone, {
|
||||||
month: "numeric",
|
month: "2-digit",
|
||||||
day: "numeric",
|
day: "2-digit",
|
||||||
year: "numeric",
|
year: "numeric",
|
||||||
}).formatToParts(date);
|
}).formatToParts(date);
|
||||||
const values = {
|
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 {
|
function formatDateTime(date: DateInput, options: DateFormatOptions = {}): string {
|
||||||
return formatValidDate(
|
return formatValidDate(
|
||||||
date,
|
date,
|
||||||
|
|
@ -161,7 +161,7 @@ function formatDateWithMonth(date: DateInput, options: DateFormatOptions = {}):
|
||||||
return formatValidDate(date, (validDate) => formatConfiguredDateWithMonth(validDate, options));
|
return formatValidDate(date, (validDate) => formatConfiguredDateWithMonth(validDate, options));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1/10/2026
|
// 01/10/2026
|
||||||
function formatDate(date: DateInput, options: DateFormatOptions = {}): string {
|
function formatDate(date: DateInput, options: DateFormatOptions = {}): string {
|
||||||
return formatValidDate(date, (validDate) => formatConfiguredDate(validDate, options, true));
|
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));
|
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 {
|
function formatShortDateTime(date: DateInput, options: DateFormatOptions = {}): string {
|
||||||
return formatValidDate(
|
return formatValidDate(
|
||||||
date,
|
date,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue