【问题标题】:Javascript UTC timezone does not work in FirefoxJavascript UTC 时区在 Firefox 中不起作用
【发布时间】:2020-08-19 18:03:42
【问题描述】:

下面的代码不适用于 Firefox,但在 Chrome 上运行良好。有人可以帮我找到替代解决方案吗?

const tempDate = getStartDate['startDate']; // Returns: 2020-08-13 12:52:38
new Date(`${tempDate} UTC`);

Expected Output: Thu Aug 13 2020 08:52:38 GMT-0400 (Eastern Daylight Time)

【问题讨论】:

标签: javascript datetime timezone utc


【解决方案1】:

你正在尝试做的事情:

new Date('2020-08-13 12:52:38 UTC')

你应该这样做:

new Date('2020-08-13T12:52:38Z')

https://www.ecma-international.org/ecma-262/11.0/index.html#sec-date-time-string-format

【讨论】:

    【解决方案2】:

    也许你可以使用时刻:https://www.npmjs.com/package/moment

    var d = new Date(`Wed Aug 12 2020 18:34:35 GMT-0400 (Eastern Daylight Time) UTC`);
    
    var date = moment(d).utc().format()
    
    console.log("date: ", date )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-03
      • 2014-09-01
      • 2011-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多