javascript - toLocaleDateString 时间格式问题(Firefox 与 Chrome)
问题描述
我在 Chrome 和 Firefox 中看到此日期格式命令的不同结果:
new Date(1156550400000).toLocaleDateString('en-us', {weekday: 'long', month: 'short', day: 'numeric', hour: '2-digit', hour12: false, timeZone: 'UTC'})+'z'
在 Firefox 中,我得到了这个结果(这是所需的输出格式):
Saturday, Aug 26, 00z"
在 Chrome 中,我得到了这个结果:
"Saturday, Aug 26, 24z"
很容易检查“24”值并替换为“00”,但希望有一个内置方法......我在 toLocaleDateString 选项中是否缺少用于格式化的选项,或者获取的替代方法想要的格式?
解决方案
我看到选项 'hour12' 在 Mozilla Firefox 和 Google Chrome 中的工作方式不同。在这种情况下,使用选项“ hourCycle ”而不是“hour12”可能会有所帮助。
new Date(1156550400000).toLocaleDateString('en-us', {weekday: 'long', month: 'short', day: 'numeric', hour: '2-digit', hourCycle: "h23", timeZone: 'UTC'})+'z'
在 Mozilla Firefox(82.0(64 位))中,这将返回"Saturday, Aug 26, 00z"
. 在 Google Chrome(版本 86.0.4240.111(官方构建)(64 位))中,这将返回"Saturday, Aug 26, 00z"
.
推荐阅读
- spring-boot - Grails got error when war file deployed to Heroku (Factory method 'requestMappingHandlerAdapter' threw exception)
- c# - ASP.NET Core MVC:无法添加管理员角色
- reactjs - 使用 create-react-app 为反应应用程序配置 jest 和酶
- symfony - Symfony 版本从 3.4 升级到 4.3 使用旧的文件夹结构和错误
- node.js - 节点 - 查找任意变量的值
- javascript - 如何在 epoch js 中执行代码,因为我是新手,我想执行一些示例图表,任何人都可以帮助找到结果
- reporting-services - SSRS 抑制导出 CSV 中的空白行
- docker - 如何防止和回收 /var/lib/docker/overlay2/hash/merged 上的 Docker EC2 磁盘空间?
- c# - 如何在没有硬编码的情况下删除文件夹的所有拒绝权限?
- java - 使用 Spark 和 Java 读写不同的 Mongo 集合