MS Edge中Intl.DateTimeFormat返回24:19的24小时制时间异常问题
Edge浏览器中Intl.DateTimeFormat 24小时制格式化异常修复
问题描述
使用以下JavaScript代码将GMT时间转换为Asia/Kolkata时区的24小时制时间时,其他浏览器输出符合预期的Sat, 4 Nov, 2023, 00:19 IST,但Microsoft Edge浏览器错误输出Sat, 4 Nov, 2023, 24:19 IST:
const gmtDate = new Date("2023-11-03T18:49:27.652173+00:00"); // GMT时间 const localTimeZone = "Asia/Kolkata"; const options = { weekday: 'short', year: 'numeric', day: 'numeric', month: 'short', hour: '2-digit', minute: '2-digit', hour12: false, timeZoneName: 'short', timeZone: localTimeZone, }; const dateFormatter = new Intl.DateTimeFormat(navigator.language, options); const localDateStr = dateFormatter.format(gmtDate); console.log(localDateStr);
解决方案
方案1:手动修正格式化结果
由于Edge在处理午夜时分的24小时制格式化时存在bug,可通过字符串替换强制将24:替换为00:,确保输出符合标准24小时制(0-23):
const gmtDate = new Date("2023-11-03T18:49:27.652173+00:00"); const localTimeZone = "Asia/Kolkata"; const options = { weekday: 'short', year: 'numeric', day: 'numeric', month: 'short', hour: '2-digit', minute: '2-digit', hour12: false, timeZoneName: 'short', timeZone: localTimeZone, }; const dateFormatter = new Intl.DateTimeFormat(navigator.language, options); let localDateStr = dateFormatter.format(gmtDate); // 修复Edge的24小时制显示异常 localDateStr = localDateStr.replace(/24:/g, '00:'); console.log(localDateStr);
方案2:更新Edge到最新版本
该问题属于Edge旧版本中Intl.DateTimeFormat的实现bug,微软后续版本已修复此问题。更新步骤:
- 打开Edge浏览器,点击右上角的三个点图标
- 选择「帮助和反馈」→「关于Microsoft Edge」
- 浏览器会自动检查并安装最新版本,完成后重启浏览器即可
内容的提问来源于stack exchange,提问作者vikalp rusia
相关产品推荐
相关产品推荐

