Reputation: 1555
Hy guys.
How can I compare this data...
var str = '09:29';
var parts = str.split(':');
minutes = parts[1];
hour = parts[2];
with actual hour?
I have a table with that value and want to compare if it is 5 minutes older than the actual time.
Thks folks.
Upvotes: 1
Views: 150
Reputation: 644
This code will be useful to get the actual time:
var d = new Date();
var curr_hour = d.getHours();
var curr_min = d.getMinutes();
Now you can either compare the hours and see if they are the same (or within 1 of each other to account for a difference in minutes), or you can do it mathematically, which is how I would do it.
if((((curr_hour * 60) + curr_min) == ((hour * 60) + minutes) + 5)){
//Do something
}
This is how I would go about doing that. I'd seperate the arithmetic logic out of the if statement though.
Edit: Here is the code I would probably use.
var givenTime = (hour * 60) + minutes;
var systemTime = (curr_hour * 60) + curr_min;
var difference = Math.abs((givenTime + 5) - systemTime);
if(difference == 0){
//do something
}
Upvotes: 1