简体   繁体   中英

Concatenate date and time string into mongo date object

I am sending two fields from client side date and time .

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 .

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.

Tried some random things using moments.js but unable to get what I want.

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. This seems to answer your question.

You can find more on dates in the MDN documentation and MongoDB documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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