When i write data in mongodb for the recording date i use new Date() in node.js and i return that date with ajax response. To calculate the time elapsed from the moment the data in mongodb i create a new date on the client side. Then i calculate the difference between the current date and the date of which came from the server.
Here is my code:
var now = new Date();
var post_date = new Date(date_from_server);
var elapsed = now - post_date/1000;
document.write(elapsed + " seconds elapsed");
But, unknown to me, the recording date is greater than the current date, and the difference between them for the first time has a value of -40 seconds!
Please help me to understand this point. I guess that's something I'm doing wrong. But what's wrong?
It seems that the time setup between the server and client is different. You may try to emit a getFinalTime event to server, where the second time is recorded and returned to client for calculation.