问题
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