I have 2 timestamps in JavaScript. One timestamp comes from an Ajax call to my database and the other is generated when the script it loaded.
What I want to do is check to see if the timestamp from the database is older than 24 hours compared the timestamp that was just generated.
In my case the database timestamp is always going to be lesser than the current timestamp.
Would this work?
var currentTimestamp = Math.round(new Date().getTime() / 1000);
if((currentTimestamp - databaseTimestamp) >= 86400) {
// Older than 24 hours
} else {
// Less than 24 hours
}
… or is there a better way?
You should rely only on your server time, as the PC may have a wrong date/time.
In your ajax call you should get both values, the timestamp from the database, and the server time of the ajax call. I why not, directly the difference.