javascript - Intl.DateTimeFormat 返回超过 24 小时的时间

标签 javascript date

我有以下 Unix 时间戳:1611328500000 (Fri Jan 22 2021 10:15:00 GMT-0500 (Eastern Standard Time))。
我需要以韩国标准时间显示。为此,我使用 Intl.DateTimeFormat .但是,由于某种原因,我得到的结果是 24:15 当我尝试格式化它时。除非我有错觉,否则我很确定这比通常的 24 小时制(0:00 到 23:59)要高。
谷歌告诉我我的结果应该是 0:15 ,显然在下一个日期(1 月 22 日星期六)。
这是一个最小的工作示例:

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

我疯了吗?在某些情况下,时间可以达到 24:15 吗?这里发生了什么?

编辑 : 我刚找到this page这似乎遇到了类似的问题。那里提供的答案将我指向一个叫做 hourCycle 的东西。 ,带有指向 MDN 的 Intl.DateTimeFormat 的链接.
但是,hourCycle仅在该页面的浏览器支持部分中出现一次。添加建议的 hourCycle: h11给我的timeOptions不工作。
进一步挖掘,我发现 this page ,其中列出了 h23作为一个有效的选项。这当然是我所追求的!但是,唉……我的结果仍然是 24:15。

最佳答案

hour12: true 切换至hourCycle: 'h23'显示从 00:00 开始的小时数至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));

关于javascript - Intl.DateTimeFormat 返回超过 24 小时的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65604554/

相关文章:

javascript - 如何在 AngularJS 的 ng-repeat 循环中使用 ng-model?

javascript - jQuery - 查找并替换子字符串

mysql - 如何在 36 秒内查找并分组时间链?

php - 在 MySql 数据库中查询当前日期后 30 天内具有 'Birthday' 日期字段的条目

javascript - 范围输入 WP 定制器上出现无效值错误

JavaScript 将对象添加到数组并进行迭代

javascript - 将字符串转换为 JavaScript 日期

php - 从 MySQL 到 Highcharts 的日期数据

Java 日期 : Long to Date Striping off Time

javascript - 使用 lodash 从数组中返回对象键和值