Moment.Js:使用UTC和时区偏移的偏移日期

编程入门 行业动态 更新时间:2024-10-26 05:28:31
本文介绍了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和时区偏移的偏移日期

本文发布于:2023-10-22 08:37:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1517003.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时区   日期   Moment   Js   UTC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!