【问题标题】:How to only change the hour and seconds portion of a utc timestamp?如何仅更改 utc 时间戳的小时和秒部分?
【发布时间】:2015-12-16 20:04:00
【问题描述】:

我有一个以毫秒为单位的 UTC 时间戳。 它表示格林威治标准时间某一天的 16:00。

时间戳:1450281600000

我只想修改该值的小时、分钟部分并返回新值。

例如 16:3059400000 但它没有日期和年份。

如何正确更改 utc 戳记?

我正在使用 Javascript 编程。

【问题讨论】:

    标签: javascript timestamp utc


    【解决方案1】:

    将您的时间戳放入 Date 对象,使用日期函数对其进行操作,然后使用 valueOf 再次返回时间戳。

    var d = new Date(1450281600000);
    d.setHours(1);
    d.setMinutes(30);
    alert(d.valueOf()); // 1450247400000

    【讨论】:

      【解决方案2】:

      你会想使用standard Date object

      例如,要将当天的 16:00 更改为 16:30,您可以这样做:

      dt = new Date(1450281600000); // instatiates Date from timestamp
      // Wed Dec 16 2015 17:00:00 GMT+0100 (CET) in my local representation
      dt.getMinutes(); // will return 0
      dt.setMinutes(30);
      // dt now is represented as 1450283400000 timestamp...
      dt.getTime(); // ...which you can see here.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-05-23
        • 2021-04-02
        • 1970-01-01
        • 2020-08-14
        • 2013-10-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多