Reputation: 45
I want to fetch the ISO Date which is stored as "string" format in Mongodb and convert it to Date format in Nodejs. Inside API I want to try
app.get('/hrTable', jsonParser, (req, res) => {
MongoClient.connect("mongodb://localhost:27017/CSS_RAG", {
useUnifiedTopology: true }, function (err, db) {
if (err) reject(err);
var dbo = db.db("CSS_RAG");
var query = {};
for (var key in req.query) {
req.query[key] !== "" ? query[key] = req.query[key] : null;
}
dbo.collection("hrTable").find(
query,
{ sort: { Date: -1 } }).toArray(function (err, result) {
if (err) reject(err);
db.close();
resolve(result);
res.send(result);
}
)})})
Upvotes: 1
Views: 112
Reputation: 716
Try this once
date = new Date(ISODate("1960-07-30T00:00:00.000+0000"));
date.getFullYear()+'-' + (date.getMonth()+1) + '-'+date.getDate();
Upvotes: 0