moment timezone adjust timestamp to a timezone and return value

梦想的初衷 提交于 2019-12-02 19:02:54

问题


I try to modify timestamps timezone with moment-timezone and I want to get the modified value with the applied offset

var newTimestamp = momentTz.tz(timestamp, 'Europe/Berlin')

so here is what I try

    var results = {};

    for (var timestamp in timestamps) {
         var commitCount = timestamps[timestamp];
         console.log(typeof timestamp)
         console.log(timestamp)
         console.log(moment(timestamp).format())
         //console.log(moment.tz(timestamp, 'Europe/Berlin'))
         //results[ moment.tz(timestamp, 'Europe/Berlin')] = commitCount;
   };

here is a snippet from log

string
1528063200
Invalid date

How do I get the new timestamp?


回答1:


It looks like your timestamp values are strings that contain Unix Time values, in terms of seconds. You can parse them with the X format specifier in moment.

However, you cannot get a timestamp in this format that is adjusted for a time zone. That is because Unix Time values are always based on UTC. It is nonsensical for them to be in a time zone. Any such value would be in error.

You can however emit a string-based format, such as ISO 8601 - which is the default when you don't supply any arguments to the format function.

In short:

moment.tz('1528063200', 'X', 'Europe/Berlin').format() //=> '2018-06-04T00:00:00+02:00'


来源:https://stackoverflow.com/questions/50604431/moment-timezone-adjust-timestamp-to-a-timezone-and-return-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!