在不同时区的同一日期 [英] Same date in different time zone

查看:151
本文介绍了在不同时区的同一日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是如何在不同时区获得同一天,一个月,一个月,一小时,几分钟,几秒钟,例如:

My question is how can I get the same day, month, year, hour, minutes, seconds in a different time zone, for example:

var now = moment().valueOf();
var result1 = moment(now).format('DD-MM-YYYY HH:mm:SS Z');

在我的时区,我得到一些这样的:

In my time zone I get some this like this:

18-02-2015 21:08:34 +01:00

所以如何更改时区而不改变其他值(天,月,...,分,...)

So how can I change only time zone without changing other values (days, months, ..., minutes, ...)

想得到这样的东西:

    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

提前感谢

推荐答案

以下是您可以问:

Here's how you could do what you are asking:

// 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
another.utcOffset('+05:30');

// shift the moment by the difference in offsets
another.add(now.utcOffset() - another.utcOffset(), 'minutes');

// 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"

但是,你必须识别两个重要的事情:

However, you must recognize two important things:


  • 另一个对象否更长的时间表示当前时间 - ,即使在目标时区。这是一个完全不同的时间。 (世界不同步本地时钟,如果是这样,我们不需要时区!)。

  • The another object no longer represents the current time - even in the target time zone. It's a completely different moment in time. (The world does not synchronize local clocks. If it did, we'd have no need for time zones!).

因此,即使上述代码满足有问题的问题,我强烈建议不要使用它。相反,重新评估您的要求,因为它们可能会误解时间和时区的性质。

For this reason, even though the above code satisfies the question that was asked, I strongly recommend against using it. Instead, re-evaluate your requirements, as it's likely they are misunderstanding the nature of time and time zones.

时区无法用偏移量完全表示单独。请阅读时区标签wiki 中的Time Zone!= Offset。虽然一些时区具有固定的偏移量(例如印度使用的+05:30),但是许多时区在一年中的不同点更改其偏移量,以适应​​夏令时

A time zone cannot be fully represented by an offset alone. Read "Time Zone != Offset" in the timezone tag wiki. While some time zones have fixed offsets (such as +05:30 used by India), many time zones change their offsets at different points throughout the year to accommodate daylight saving time.

如果你想说明这一点,你可以使用 moment-timezone ,而不是调用 utcOffset(...)。但是,我的第一个项目符号中的问题仍然适用。

If you wanted to account for this, you could use moment-timezone instead of calling utcOffset(...). However, the issue in my first bullet would still apply.

// 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
another.tz('America/New_York'); // or whatever time zone you desire

// shift the moment by the difference in offsets
another.add(now.utcOffset() - another.utcOffset(), 'minutes');

// 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"

这篇关于在不同时区的同一日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆