IE11:给定变量不是格式问题!时,新Date返回“无效日期”

问题描述

我遇到了以下问题,无法找出原因。我知道日期格式不是问题,因为我已经尝试使用RFC2822和ISO8601格式无济于事。

当我尝试从变量中获取IE11中的日期对象时,它将返回“无效日期”。即使变量是一个字符串并且格式正确。

我在一开始就定义了变量(Helper.parseDate()函数仅将破折号替换为字符串中的斜杠以使其具有正确的格式)并检查它是否真的是字符串,并在控制台再次检查格式。我什至包括了dayJS库,以检查这是否是IE11中Date()的某些未知限制。

let formattedDate = Helper.parseDate(this.date) + ' ' + this.time
console.log(typeof(formattedDate)) // string
console.log(formattedDate) // '2020/11/05 08:58:00'
console.log('variable: ',new Date(formattedDate)) // Invalid Date
console.log('variable dayjs: ',dayjs(formattedDate,'YYYY/MM/DD hh:mm:ss')) // Invalid Date
    
console.log(typeof('2020/11/05 08:58:00')) // string
console.log('2020/11/05 08:58:00')
console.log('hardcoded: ',new Date('2020/11/05 08:58:00')) // valid Date
console.log('hardcoded dayjs: ',dayjs('2020/11/05 08:58:00','YYYY/MM/DD hh:mm:ss')) // valid Date

Screenshot of results in IE11 console

现在,当我在使用变量之前为其提供一个值时,它将起作用。

let formattedDate = '2020/11/05 08:58:00'
console.log(typeof(formattedDate)) // string
console.log(formattedDate) // '2020/11/05 08:58:00'
console.log('variable: ','YYYY/MM/DD hh:mm:ss')) // valid Date

Screenshot of results in IE11 console

什么可能导致此问题?在所有其他浏览器中,它都可以正常工作。我以为我使用的辅助功能是否属于某种竞争状况,但console.logs也应该返回假值,对吧?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)