MongoDB + JS日期转换了-1天 [英] MongoDB + JS Date shifted -1 day
问题描述
我正在尝试使用以下方法在mongodb文档中添加新日期
I'm trying to add a new date inside my mongodb document using
db.collection('course').insertOne({
date: new Date(item.date)
});
item.date
来自
items.push({
date: moment(new Date(day.label)).tz('Europe/Paris').format('YYYY/MM/DD'),
});
day.label
就像'14 November 2016'
item.date
的格式类似于2016/11/14
文档插入工作,但是日期格式不正确,例如2016/11/14
Mongo插入"date" : ISODate("2016-11-13T23:00:00Z")
The document insert work but the dates are not properly formated, for example, for 2016/11/14
Mongo insert "date" : ISODate("2016-11-13T23:00:00Z")
每个插入的日期都转换为-1天
Every inserted dates are shifted -1 day
在MongoDB中插入自定义格式的日期是否正确? 我应该更改momentJs日期格式吗?
Is the proper way to insert custom formated date in MongoDB ? Should I change the momentJs date format ?
由于时区不同,日期发生了变化.
Dates are shifted because of different timezone.
推荐答案
一天没有转移,实际上根本没有转移.
It's not shifted by a day, it's not actually shifted at all.
显示的时间偏移一个小时.如果您注意到了,时间已在前一天的23:00,而不是默认为指定日期的午夜.
The time shown is shifted by an hour. If you notice, the time has become 23:00 on the previous day, rather than defaulting to midnight on the date you have specified.
更重要的是,时间戳中的Z后缀表示时区为UTC,而UTC中的2016-11-13T23:00:00
表示与时间戳2016-11-14T00:00:00+01:00
相同的时刻.因此,日期时间已正确创建,但只是在创建日期的另一个时区中显示给您.
More importantly, the Z suffix in the timestamp means that the timezone is UTC, and 2016-11-13T23:00:00
in UTC refers to the same moment in time as the timestamp 2016-11-14T00:00:00+01:00
. So the datetime is being created correctly, but just shown back to you in a different timezone that you're creating it in.
这篇关于MongoDB + JS日期转换了-1天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!