最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Intl.DateTimeFormat returns an hour over 24 - Stack Overflow

programmeradmin2浏览0评论

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.

Share Improve this question edited Jan 6, 2021 at 23:22 matthew-e-brown asked Jan 6, 2021 at 23:12 matthew-e-brownmatthew-e-brown 3,0071 gold badge12 silver badges32 bronze badges 5
  • 2 I'm seeing 00:15. – StackSlave Commented Jan 6, 2021 at 23:13
  • @StackSlave That's what I was afraid of someone seeing... i.imgur.com/vKbpVA8.png here's my result, just so people can know I'm not crazy... – matthew-e-brown Commented Jan 6, 2021 at 23:15
  • @matthew-e-brown 24:15 on my end. Curious. – Emiel Zuurbier Commented Jan 6, 2021 at 23:16
  • Firefox shows 00:15, Chrome shows 24:15. Chrome bug or Firefox bug? – evolutionxbox Commented Jan 6, 2021 at 23:21
  • 1 Chromium bug but also affects Node since both use the underlying V8 JavaScript engine – andyb Commented Aug 31, 2022 at 11:27
Add a comment  | 

2 Answers 2

Reset to default 18

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));

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
发布评论

评论列表(0)

  1. 暂无评论