简体   繁体   English

将日期和时间字符串连接到mongo日期对象

[英]Concatenate date and time string into mongo date object

I am sending two fields from client side date and time . 我从客户端发送datetime两个字段。

date will be in the format of YYYY-MM-DD ie 2016-11-08 and time will be in the format of 05:30 PM or 09:45 AM . date将采用YYYY-MM-DD的格式,即2016-11-08, time将采用05:30 PM09:45 AM的格式。

I want to combine these two fields and create new field say added_datetime , this field going to be inserted inside MongoDB , so I want it to be in the form of Mongo Date Object so that I can use it for searching with date. 我想结合这两个字段并创建一个新字段,例如added_datetime ,该字段将插入MongoDB ,所以我希望它采用Mongo Date Object的形式,以便我可以用它来搜索日期。

Tried some random things using moments.js but unable to get what I want. 使用moments.js尝试了一些随机的东西,但无法获得我想要的东西。

As mentioned in a similiar question you can create a date object with 类似问题中所述,您可以使用以下命令创建日期对象

var date = new Date(datestring);

The code 编码

var startDate = new Date("1900-1-1 8:20:00 PM");

which the original questioner supplied works in chrome and should also work in node since it's the same js engine. 原始提问者提供的内容适用于chrome,并且也应适用于node,因为它是相同的js引擎。 This seems to answer your question. 这似乎可以回答您的问题。

You can find more on dates in the MDN documentation and MongoDB documentation. 您可以在MDN文档MongoDB文档中找到有关日期的更多信息。

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

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