Moment.Js:使用UTC和时区偏移的偏移日期 [英] Moment.Js: Offsetting dates using UTC and Timezone offset

查看:3247
本文介绍了Moment.Js:使用UTC和时区偏移的偏移日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用时区偏移量和UTC时间戳调整时间。

I am trying to adjust a time using a timezone offset and a UTC timestamp.

我正在运行以下代码:

var date = {
    utc: '2013-10-16T21:31:51',
    offset: -480
}

var returnDate = moment(date.utc).utc().zone(date.offset).format('MM/DD/YYYY h:mm A');

我期待的是: 10/16/2013 1:31 PM 但是我以$ code为结束10/17/2013 9:31 AM

What I am expecting is: 10/16/2013 1:31 PM but I am ending up with 10/17/2013 9:31 AM

推荐答案

这对我有用:

var date = {
  utc: '2013-10-16T21:31:51',
  offset: 480
}

var returnDate = moment.utc(date.utc).zone(date.offset).format('MM/DD/YYYY h:mm A');

如果您注意到,我将偏移量更改为正数。这给出了期望的结果。如果偏移量保留在 -480 ,则输出为 10/17/2013 5:31 AM

If you noticed, I changed the offset to a positive number. This gave the desired result. If the offset was left at -480 the output was 10/17/2013 5:31 AM.

有一个 moment#UTC 将日期初始化为UTC与本地时间的方法。

There is a moment#UTC method that initializes the date as UTC vs. local time.

这篇关于Moment.Js:使用UTC和时区偏移的偏移日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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