【问题标题】:JS timezone conversionJS时区转换
【发布时间】:2023-03-19 10:13:01
【问题描述】:

在 Javascript 中,我有一个我正在处理的时间戳:

var origUnixTimestamp = (date * 1000);

除了这个时间戳,我还有一个 UTC 偏移量(-5,虽然这是可变的)。我希望使用 Date 的 getTimezoneOffset() 方法将 origUnixTimestamp 转换为用户的 UTC 偏移量。

我只是想知道如何考虑原始时间戳 UTC 偏移量(例如,-5)并将其转换为用户当前的 UTC 偏移量。我想这很简单,但此刻它正在扭曲我的大脑。

【问题讨论】:

    标签: javascript timestamp unix-timestamp


    【解决方案1】:

    Javascript 为你做这件事。存储在 Date 对象中的所有日期都已转换为正确的时区(只需将您的纪元传递给构造函数)。同一个 Date 对象可以使用纪元日期和 UTC。

    var some_date = new Date(epoch);
    var time = some_date.getDay(); // will be different in different zones
    some_date.setDay(22); // to set day
    var origUnixTimestamp = some_date.getTime(); //returns you epoch
    

    【讨论】:

      【解决方案2】:

      【讨论】:

        【解决方案3】:

        This link 有从本地时间转换的说明:

        // create Date object for current location
        d = new Date();
        
        // convert to msec
        // add local time zone offset
        // get UTC time in msec
        utc = d.getTime() + (d.getTimezoneOffset() * 60000);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-10-31
          • 2020-09-08
          • 2021-12-14
          • 1970-01-01
          • 1970-01-01
          • 2015-11-27
          • 2015-04-26
          • 2022-07-06
          相关资源
          最近更新 更多