Intl.DateTimeFormat returns an hour over 24

Viewed 1540

I have the following Unix timestamp: 1611328500000 (Fri Jan 22 2021 10:15:00 GMT-0500 (Eastern Standard Time)).

I need to display it in Korean Standard Time. To do so, I'm using Intl.DateTimeFormat. However, for some reason, the result I'm getting is 24:15 when I attempt to format it. Unless I'm delusional, I'm pretty sure that's higher than a 24-hour clock usually goes (0:00 to 23:59).

Google tells me my result should be 0:15, obviously on the following date (Sat Jan 22).

Here's a minimal working example:

const date = new Date(1611328500000);

const timeOptions = {
  hour12: false,
  hour: '2-digit',
  minute: '2-digit'
};

const formatter = new Intl.DateTimeFormat('en-US', {
  timeZone: 'Asia/Seoul', ...timeOptions
});

console.log(formatter.format(date));

Am I crazy? Can times go up to 24:15 in some circumstances? What is happening here?


EDIT: I just found this page which seems to be experiencing a similar problem. The answer provided there points me towards something called hourCycle, with a link to MDN's Intl.DateTimeFormat.

However, hourCycle only appears once on that page, in the browser support section. Adding the suggested hourCycle: h11 to my timeOptions did not work.

Digging further, I found this page, which lists h23 as a valid option. Surely this is what I'm after! But, alas... my result is still 24:15.

1 Answers

Switch from hour12: true to hourCycle: 'h23' to display the hours from 00:00 to 23:59.

const date = new Date(1611328500000);

const timeOptions = {
  hourCycle: 'h23',
  hour: '2-digit',
  minute: '2-digit'
};

const formatter = new Intl.DateTimeFormat('en-US', {
  timeZone: 'Asia/Seoul', ...timeOptions
});

console.log(formatter.format(date));

Related