JavaScript - How to save a date in MongoDB document in ISODate format?

不打扰是莪最后的温柔 提交于 2019-12-05 14:33:34

I solved this by handling this in my Node JS API side. The real problem is I've been sending this to the API as an stringified JSON object. though it was set as a new Date() object it get stringified.

So within my Node JS API side before inserting it into the MongoDB collection I've done this,

var data = req.body.postData;
var date = data[0].date;
var dateObject = new Date(date);
date[0].date = dateObject;

Which did the trick! Thanks for the answers!

You can try this:

var currentDate = new Date();

postData = {
   deviceID: deviceID,
   companyID: companyID,
   userID: userID,
   date: currentDate.toISOString()
};

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!