diff --git a/src/__tests__/format.spec.ts b/src/__tests__/format.spec.ts index 3bf51be..beb2115 100644 --- a/src/__tests__/format.spec.ts +++ b/src/__tests__/format.spec.ts @@ -222,4 +222,7 @@ describe("format with a timezone", () => { }) ).toBe("02:30:00+0000") }) + it("can render a double character zero with leading zeros in zh (#41)", () => { + expect(format("2022-04-10", "YYYY-MM", "zh")).toBe("2022-04") + }) }) diff --git a/src/common.ts b/src/common.ts index 3d1a9bc..e530e8f 100644 --- a/src/common.ts +++ b/src/common.ts @@ -177,11 +177,9 @@ export function fill( if (partName === "hour" && token === "H") { return value.replace(/^0/, "") } - if ( - (partName === "minute" || partName === "second") && - (token === "mm" || token === "ss") && - value.length === 1 - ) { + if (["mm", "ss", "MM"].includes(token) && value.length === 1) { + // Some tokens are supposed to have leading zeros, but Intl doesn't + // always return them, depending on the locale and the format. return `0${value}` } if (partName === "dayPeriod") {