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
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;
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";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With