简体   繁体   English

MongoDB的日期格式(使用Mongoose插入)

[英]Date format of MongoDB (inserting using Mongoose)

This is a 2 question post: 这是一个2个问题的帖子:

When I enter a date for a document into MongoDB through an html form, I'll enter it using this format: YYYY-MM-DD 当我通过html表单将文档的日期输入MongoDB时,我将使用以下格式输入: YYYY-MM-DD

For example: 2014-02-13 例如: 2014-02-13

However, in MongoDB, it inputs it in the ISOdate format, and while the YYYY-MM-DD will be correct, for whatever reason it will insert one of 3 different hour time stamps: 但是,在MongoDB中,它以ISOdate格式输入,而YYYY-MM-DD将是正确的,无论出于何种原因,它将插入3个不同的小时时间戳中的一个:

ISODate("2016-02-02T00:00:00.000Z")

ISODate("2015-02-16T05:00:00.000Z")

ISODate("2016-08-02T04:00:00.000Z")

I never specify the hour when entering dates, but for whatever reason it will always insert into MongoDB an hour of 0, 4, or 5. 我从不指定输入日期的小时数,但无论出于何种原因,它总是会在0到4或5小时内插入MongoDB。

I have no idea why it's doing this. 我不知道它为什么这样做。

Alternatively, is there a way to remove the 'time' part of the date, seeing as I don't need it? 或者,有没有办法删除日期的“时间”部分,因为我不需要它? For 'date' all I need is YYYY-MM-DD. 对于“约会”,我需要的是YYYY-MM-DD。

要仅将日期作为日期对象返回,您可以尝试以下操作:

var myDate = new Date("2016-05-18T16:00:00Z");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM