JsCoder
JsCoder

Reputation: 1733

toLocaleDateString returns unexpected formatted time

The below call returns 24:00 in latest Chrome & Opera, while it previously returned 00:00, is this a by design behavior?

const [, time] = new Date(2020, 1, 1, 0, 0).toLocaleDateString("en-us",
        {
            hour12: false,
            hour: "2-digit",
            minute: "2-digit"
        }).split(", ");

console.info(time); // 24:00

Upvotes: 10

Views: 1548

Answers (2)

phuzi
phuzi

Reputation: 13060

Use hourCycle instead of hour12 and set it to h23.

const [, time] = new Date(2020, 1, 1, 0, 0).toLocaleDateString("en-us",
        {
            hourCycle: "h23",
            hour: "2-digit",
            minute: "2-digit"
        }).split(", ");

console.info(time); // 00:00

Upvotes: 8

T.J. Crowder
T.J. Crowder

Reputation: 1074168

It looks to me like Chrome (or its V8 engine) has updated to match the specification, which says in Step 18(e)(vi):

If p is "hour" and dateTimeFormat.[[HourCycle]] is "h24", then If v is 0, let v be 24.

That specification hasn't changed, but it looks like they must have fixed a bug. (I didn't immediately find one in the V8 or Chromium issue list, but...)

Interestingly, Firefox shows 00:00, not 24:00.

Upvotes: 2

Related Questions