Skip to content

Commit

Permalink
fix: leading zeros for MM in zh locale (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-schroeder committed Mar 26, 2024
1 parent 954533a commit 9687854
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/__tests__/format.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
})
8 changes: 3 additions & 5 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down

0 comments on commit 9687854

Please sign in to comment.