Skip to content

Commit 9687854

Browse files
fix: leading zeros for MM in zh locale (#41)
1 parent 954533a commit 9687854

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

src/__tests__/format.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,7 @@ describe("format with a timezone", () => {
222222
})
223223
).toBe("02:30:00+0000")
224224
})
225+
it("can render a double character zero with leading zeros in zh (#41)", () => {
226+
expect(format("2022-04-10", "YYYY-MM", "zh")).toBe("2022-04")
227+
})
225228
})

src/common.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,9 @@ export function fill(
177177
if (partName === "hour" && token === "H") {
178178
return value.replace(/^0/, "")
179179
}
180-
if (
181-
(partName === "minute" || partName === "second") &&
182-
(token === "mm" || token === "ss") &&
183-
value.length === 1
184-
) {
180+
if (["mm", "ss", "MM"].includes(token) && value.length === 1) {
181+
// Some tokens are supposed to have leading zeros, but Intl doesn't
182+
// always return them, depending on the locale and the format.
185183
return `0${value}`
186184
}
187185
if (partName === "dayPeriod") {

0 commit comments

Comments
 (0)