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

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

问题描述

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

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

但是,您必须认识到两件重要的事情:

However, you must recognize two important things:

  • another 对象不再代表当前时间 - 即使在目标时区.这是一个完全不同的时刻.(世界不会同步本地时钟.如果同步了,我们就不需要时区了!).

  • 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 中阅读时区!= 偏移量".虽然某些时区有固定的偏移量(例如印度使用的 +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 - 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"

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

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