Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 854
user1514042 Avatar asked Mar 12 '20 13:03

user1514042


2 Answers

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
like image 138
phuzi Avatar answered Oct 17 '22 06:10

phuzi


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.

like image 1
T.J. Crowder Avatar answered Oct 17 '22 06:10

T.J. Crowder