MongoDB + JS日期转换了-1天 [英] MongoDB + JS Date shifted -1 day

查看:283
本文介绍了MongoDB + JS日期转换了-1天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下方法在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屋!

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