JavaScript Date 对象在 Firefox 浏览器上出现时区错误



我需要使用时区CST/CDT创建javascript对象,下面的代码行在chrome中工作正常,但在Firefox上则不行

new Date("2020-06-06 05:37:34.0" + ' CST/CDT');

铬结果:

Sat Jun 06 2020 15:37:34 GMT+0500 (Pakistan Standard Time)

火狐结果:

Invalid Date

需要建议来修复它。

始终尝试使用 UTC 日期,这意味着以 UTC 格式存储和执行日期计算。

如果要创建特定于时区的Date对象,请执行以下操作,我已经展示了如何将其转换回UTC/本地时区:

const date = new Date('2010-05-11T10:11:00-0500');//CDT time
console.log(date.toString()); //date as per the current timezone of the machine this code is executed => "Tue May 11 2010 20:41:00 GMT+0530 (India Standard Time)"
console.log(date.toISOString());//converts the Date as UTC based ISO date string => "2010-05-11T15:11:00.000Z"

最新更新