不同时区的相同日期

问题描述 投票:0回答:5

我的问题是如何获得不同时区的相同日、月、年、时、分、秒,例如:

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

提前致谢

date timezone momentjs
5个回答
62
投票

以下是您可以执行您所要求的操作:

// 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
    对象不再代表当前时间 - 即使在目标时区。这是一个“完全不同”的时刻。 (世界并不同步本地时钟。如果同步,我们就不需要时区了!)。 因此,即使上面的代码满足了提出的问题,

    我强烈建议不要使用它

    。相反,重新评估您的要求,因为他们很可能误解了时间和时区的性质。

  • 时区不能仅用偏移量来完全表示。阅读
  • 时区标签 wiki

    中的“时区 != 偏移量”。虽然某些时区有固定的偏移量(例如印度使用的 +05:30),但许多时区在一年中的不同时间点会更改其偏移量,以适应夏令时

  • 如果您想解释这一点,您可以使用
  • moment-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"


12
投票

const myTime = moment.tz('2016-08-30T22:00:00', moment.ISO_8601, 'America/Denver') myTime.format() //2016-08-30T22:00:00-06:00 const sameTimeDifferentZone = moment.tz(myTime.format('YYYY-MM-DDTHH:mm:ss.SSS'), moment.ISO_8601, 'America/New_York') sameTimeDifferentZone.format() //2016-08-30T22:00:00-04:00



7
投票

var newTimezone = 'America/Denver'; //date - contains existing moment with timezone i.e 'America/New_York' moment.tz(date.format('YYYY-MM-DDTHH:mm:ss'), 'YYYY-MM-DDTHH:mm:ss', newTimezone);



2
投票
http://momentjs.com/timezone/docs/

参考

moment-timezone-with-data.js

并指定要前往哪个时区,如下所示: moment(date).tz("America/Los_Angeles").format()



0
投票

© www.soinside.com 2019 - 2024. All rights reserved.