I have dates stored in MongoDB using Date(), MongoDB uses UTC it’s date type, and as a string looks like Mon, 02 Apr 2012 20:16:31 GMT.
I want to get the difference in time, in total seconds, between this time and the current time (in UTC).
I’ve tried this:
now = new Date();
current_date = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());
end_date = obj.end_date (Mon, 02 Apr 2012 20:16:35 GMT);
d = new Date(end_date - current_date);
console.log(d.getSeconds());
This is displaying 22 for seconds, which obviously isn’t right.
It seems over complicated too. Maybe there’s a better way within MongoDB or a correct way within JavaScript to do this?
Any suggestions would be great – thank you!
You could use getTime() on the Date objects to get the difference in milliseconds then divide the difference by 1000;
e.g.