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));
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.
2 Answers
Reset to default 18Switch 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));
I used the below code and it worked to transform the input 3000000
milliseconds to the output 00:50:00.000Z
const dateInMilliseconds = 3000000
const formatterConfig = {
hour: "numeric",
minute: "numeric",
second: "numeric",
hourCycle: "h23",
timeZone: "UTC",
fractionalSecondDigits: 3
}
const dateInFormat = new Intl.DateTimeFormat([], formatterConfig).format(dateInMilliseconds)+'Z';
// 00:50:00.000Z
00:15
. – StackSlave Commented Jan 6, 2021 at 23:1324:15
on my end. Curious. – Emiel Zuurbier Commented Jan 6, 2021 at 23:1600:15
, Chrome shows24:15
. Chrome bug or Firefox bug? – evolutionxbox Commented Jan 6, 2021 at 23:21