我的问题是如何在不同的时区获得相同的日、月、年、时、分、秒,例如:
var now = moment().valueOf();
var result1 = moment(now).format('DD-MM-YYYY HH:mm:SS Z');
18-02-2015 21:08:34 +01:00
result2: 18-02-2015 21:08:34 +01:00
result3: 18-02-2015 21:08:34 +10:00
result4: 18-02-2015 21:08:34 +05:00
result5: 18-02-2015 21:08:34 -06:00
result6: 18-02-2015 21:08:34 -11:00
最佳答案
以下是您可以按照您的要求执行的操作:
// get a moment representing the current time
var now = moment();
// create a new moment based on the original one
var another = now.clone();
// change the offset of the new moment - passing true to keep the local time
another.utcOffset('+05:30', true);
// log the output
console.log(now.format()); // "2016-01-15T11:58:07-08:00"
console.log(another.format()); // "2016-01-15T11:58:07+05:30"
another
对象 不再代表当前时间 - 即使在目标时区。这是一个完全不同的时刻。 (世界不会同步本地时钟。如果同步了,我们就不需要时区了!)。utcOffset(...)
.但是,我的第一个项目符号中的问题仍然适用。 // get a moment representing the current time
var now = moment();
// create a new moment based on the original one
var another = now.clone();
// change the time zone of the new moment - passing true to keep the local time
another.tz('America/New_York', true); // or whatever time zone you desire
// log the output
console.log(now.format()); // "2016-01-15T11:58:07-08:00"
console.log(another.format()); // "2016-01-15T11:58:07-05:00"
https://stackoverflow.com/questions/28593304/