Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript check if date object is at least 5 minutes old (nodeJS) [duplicate]

How to compare a date object in javascript to check if the object is at least 5 minutes old? I tried:

//current date
var date = new Date();
//data from database
console.log("db: " + data_from_database)
console.log("current date: " + date)

//how to check if "data_from_database is at least 5 minutes old?

db: Sun Jul 16 2017 23:59:59 GMT+0200 (CEST)

current date: Sun Jul 16 2017 14:12:43 GMT+0200 (CEST)

How to perform the check the simplest way without creating tons of new objects?

like image 740
seikzer Avatar asked Dec 07 '22 17:12

seikzer


1 Answers

You would get the time in milliseconds when you subtract two dates and then you can compare

var date = new Date();
//data from database
console.log("db: " + data_from_database)
console.log("current date: " + date)

var FIVE_MIN=5*60*1000;

if((date - new Date(date_from_database)) > FIVE_MIN) {
   console.log('Delayed by more than 5 mins');
}
like image 159
Shubham Khatri Avatar answered Dec 10 '22 05:12

Shubham Khatri