I have two time data here. one is from mongodb database which i finded by objectId. other one is currenttime. i put both in variables.. now i want to subtract the time value of database from current time.. here is some code which i tried.
var mongo = require('mongodb');
var ObjectId = require('mongodb').ObjectID;
var lastUpdate = new Date();
var id = ObjectId("5a5ee7588a6a102398be8ada");
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/blue";
MongoClient.connect(url, function (err, db) {
if (err)
throw err;
var dbase = db.db("blue");
dbase.collection("screams").findOne({ email: "Adnan@gmail.com", _id: id },
function (err, searchtype) {
if (err)
throw err;
var resdate = searchtype.createdAt;
console.log(resdate);
dbase.collection("screams").find(function () {
return (this.lastUpdate.valueOf() - this.resdate.valueOf())
> (1000 * 60 * 60 * 24);
})
db.close();
});
});
MongoDb has $subtract operator in its aggregation framework can help you to achieve this purpose I think.
Try
dbase.collection('screams').aggregate([
{$match:{email:"Adnan@gmail.com", _id: id }},
{$project:{_id:1, email:1, createdAt:1,
datediff:{$subtract:[new Date(), '$createdAt']}}
}
], function(err, result) {
//Do what you need to do here.
})
The datediff is in unit of milliseconds, you might need to do some conversion there to meet your need.
luckily I found a simple solution. use it if any body confronted with same problem
var d1 = new Date(); , // put ur any date value in the given variable
d2 = new Date();
var diff = Math.abs(d2 - d1);
console.log(diff);
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.