代码之家  ›  专栏  ›  技术社区  ›  F.I.

JS时间戳错误-时区设置[已关闭]

  •  0
  • F.I.  · 技术社区  · 2 年前

    当使用JS从时间戳设置日期时,我获得了错误的小时数。

    var timestamp = 1548374400000 //In milliseconds.
    var time_string =  new Date(timestamp).toLocaleString("en-GB",{timezone:"Europe/London"})
    console.log(time_string)
    

    哪些输出:

    25/01/2019, 01:00:00
    

    但是,在以下网站上查看时: https://www.epochconverter.com/timezones?q=1548374400000&tz=Europe%2FLondon

    我获得的时间戳(1548374400000)对应于日期:2019年1月25日星期五00:00:00(上午),欧洲/伦敦时区(GMT)

    这些结果之间有一个小时的差距,我无法解释。

    我已经在这个网站上检查了一下,以确定这是我这边的一个错误: enter image description here

    1 回复  |  直到 2 年前
        1
  •  0
  •   Strg C    2 年前
    var timestamp = 1548374400000; // In milliseconds.
    var time_string = new Date(timestamp).toLocaleString("en-GB", { timeZone: "Europe/London" });
    console.log(time_string);
    

    你只是用错了关键词。使用上面的代码应该可以解决您的问题。