首页 > 解决方案 > 为什么 Intl.DateTimeFormat 在 IE 浏览器中不起作用

问题描述

我有这个 JS 功能:

var TodayDate = new Intl.DateTimeFormat(en-US, { day: 'numeric', month: 'long', year: 'numeric' }).format(Date.now()) 

输出是:

铬:2020 年 4 月 19 日

边缘:2020 年 4 月 19 日

火狐 = 2020 年 4 月 19 日

但在 IE(Internet Explorer)中根本不起作用,为什么?

标签: javascript

解决方案


它仅适用于 Internet Explorer 11,没有更早的版本。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat/DateTimeFormat

在此处输入图像描述

toLocaleDateString应该给出相同的结果,并且在 IE 5.5+ 上可用

const dateTimeFormat = new Intl.DateTimeFormat('en-US', { day: 'numeric', month: 'long', year: 'numeric' }).format(Date.now()) 
console.log(dateTimeFormat)

const localeDateString = new Date().toLocaleDateString('en-US', { day: 'numeric', month: 'long', year: 'numeric' })
console.log(localeDateString)


推荐阅读