Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine how many minutes has passed in javascript

I have variable which I populate from JSON formated data. Something like: var time=my_data[data.results[i].id].time; This gives me time from database in this format : 2012-06-19 15:48:18.140

How can save in some other variable value for example active if the time between a value that I get from database and time which is present(now) is less then 5 min and inactive if it is more then 5 min passed

Thank you

like image 245
user123_456 Avatar asked Jan 16 '23 07:01

user123_456


2 Answers

See the DOC for javascript Date

Date Constructor accepts a date string. I tried yours in chrome console it works fine. Then you can use getTime to get number of milliseconds since January 1, 1970

Finally

var past = new Date(yourTimeString).getTime();
var fiveMin = 1000 * 60 * 5; 
var isPast = (new Date().getTime() - past < fiveMin)?false:true;
like image 107
Tamil Avatar answered Jan 19 '23 00:01

Tamil


var fiveMinutes = 1000 * 60 * 5;
var otherVariable = ((new Date().getTime() - time) < fiveMinutes) ? "active": "inactive";

But I'm unsure of the type you get from your JSON, so you might need to use this variant instead :

var fiveMinutes = 1000 * 60 * 5;
var otherVariable = ((new Date().getTime() - new Date(time).getTime()) < fiveMinutes) ? "active": "inactive";
like image 44
Romain Valeri Avatar answered Jan 19 '23 00:01

Romain Valeri