MomentJS不能正确转换日期 [英] MomentJS not converting date correctly

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

问题描述

我得到的记录从Mongolab格式为:2016-02-07T04:11:27.667Z

I'm getting records from Mongolab with the format: "2016-02-07T04:11:27.667Z"

要显示的日期,我将它们转换为 MM-DD-YYYY ,使他们更可读的:

To display the dates, I am converting them to MM-DD-YYYY to make them more human readable:

obj.converted_date = moment(obj.date).format('MM-DD-YYYY');

由于某些原因,它的转换对2016年2月7日记录2016年2月6日:

For some reason, it's converting records on 2/7/2016 to 2/6/2016:

converted_date: "02-06-2016"
date: "2016-02-07T04:11:27.667Z"

为什么会出现这种情况?

Why does this happen?

推荐答案

这是因为日期重新在本地时间present而不是UTC,除非你要求它它们存储在:

It happens because dates represent in "local time" rather than "utc" that they are stored in unless you ask it to:

 moment(obj.date).utc().format('MM-DD-YYYY');

所以,不同的是通过重新配置的调整时区的日期psented $ P $。

So the difference is represented by your configured timezone adjusting the date.

请参阅 UTC 在docs。

这篇关于MomentJS不能正确转换日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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