我的问题是如何在不同时区获得相同的日、月、年、小时、分钟、秒,例如:
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
提前致谢
这就是你如何could做你所要求的:
// 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"
但是,您必须认识到两件重要的事情:
-
The another
object 不再代表当前时间 - 即使在目标时区。它是完全不同这一刻。 (世界并不同步本地时钟。如果同步,我们就不需要时区了!)。
因此,即使上面的代码满足了提出的问题,我强烈建议不要使用它。相反,重新评估您的要求,因为他们很可能误解了时间和时区的性质。
时区不能仅用偏移量来完全表示。阅读“时区!=偏移量”时区标签 wiki https://stackoverflow.com/tags/timezone/info。虽然某些时区有固定的偏移量(例如印度使用的 +05:30),但许多时区在一年中的不同时间点会更改其偏移量以适应夏令时 https://stackoverflow.com/tags/dst/info.
如果你想解释这一点,你可以使用时刻时区 http://momentjs.com/timezone/而不是打电话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"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)