Reputation: 47
I wrote the following script to find the time difference between two time values and execute a function when the difference is 10 minutes. In the script, variables h1, m1 and s1 are the hour, minute and second values of the first time, and h2, s2 and m2 are those of the second.
For example
h1 = 10
m1 = 55
s1 = 00
h2 = 11
m2 = 05
s2 = 00
The script currently checks to see if h1 is equal to h2. If it is, then it finds the difference between m2 and m1. If this difference is 10, it executes the function.
//........setting the current time........//
var d1 = new Date();
var h1 = d1.getHours();
var m1 = d1.getMinutes();
var s1 = d1.getSeconds();
//if the hour, minute or second is a single digit number, add a zero before it//
if (h1 < 10) {
h1 = "0"+ h1;
}
if (m1 < 10) {
m1 = "0" + m1;
}
if (s1 < 10) {
s1 = "0" + s1;
}
var now = h1 + ":" + m1 + ":" + s1;
//........setting the target time........//
var d2 = new Date(2018, 8, 16, 11, 05, 00);
var h2 = d2.getHours();
var m2 = d2.getMinutes();
var s2 = d2.getSeconds();
//if the hour, minute or second is a single digit number, add a zero before it//
if (h2 < 10) {
h2 = "0" + h2;
}
if (m2 < 10) {
m2 = "0" + m2;
}
if (s2 < 10) {
s2 = "0" + s2;
}
var time = h2 + ":" + m2 + ":" + s2;
//........Calculating the difference........//
if (h1 == h2) {
var diff = m2 - m1;
if ((diff == 10)) {
document.getElementById("diff").innerHTML = diff;
move();
}
}
However, I've realised this doesn't always work, like with the example times above. When the hour values aren't the same, the script doesn't calculate the time difference. Is there a way I can overcome this?
Upvotes: 0
Views: 1152
Reputation: 147343
You should calculate the time difference first, then do the logic. That way you can always show the difference regardless of the outcome of the test. To do the difference, convert to some common base like seconds or minutes, then format however suits for presentation, e.g.
function getTimeDiff(h1, m1, s1, h2, m2, s2) {
return diff = toSeconds(h2, m2, s2) - toSeconds(h1, m1, s1);
}
function toSeconds(h, m, s) {
return h*3600 + m*60 + s*1;
}
function secondsToHMS(secs) {
function z(n){return (n<10? '0':'') + n}
return z(secs/3600|0) + ':' +
z((secs%3600)/60|0) + ':' +
secs%60;
}
// Tests
[[4,23,15, 5,5,8], // 04:23:15 vs 05:05:08
[4,23,15, 4,25,8]]. // 04:23:15 vs 04:25:08
forEach(function(arr) {
var diff = getTimeDiff(...arr);
console.log(`Diff of ${secondsToHMS(diff)} is ${diff > 600? 'more':'less'} than 10 minutes`);
});
Upvotes: 1
Reputation: 462
To calculate the difference, you can replace whatever you've put below your "Calculating the difference" comment with:
//........Calculating the difference........//
if(Math.abs(d1.getTime() - d2.getTime()) <= 600000) {
// execute your function
}
Explanation
getTime()
function of Date
object returns the date timestamp, i.e. the number of milliseconds elapsed between the 1st Jan 1970 00:00:00 UTC and the date in your object. This is much easier to use when making computations with dates, rather than separately comparating/calculating hours, then minutes and seconds... It is also a pretty common practice in sofware development.Math.abs()
in order to ensure that this difference is always a positive number. That makes the following comparison easier.There we go! You have now the condition you need for calling your function.
Feel free to reply if more clarification is needed.
Upvotes: 0
Reputation: 1246
Try using parseInt()
function as after appending 0 to h1, h2, m1 and m2 it is treated as a string.
if (parseInt(h1) == parseInt(h2)) {
var diff = parseInt(m2) - parseInt(m1);
if ((diff == 10)) {
document.getElementById("diff").innerHTML = diff;
move();
}
}
Upvotes: 0
Reputation: 479
As answered here Get difference between 2 dates in JavaScript?
var date1 = new Date("7/13/2010");
var date2 = new Date("12/15/2010");
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
alert(diffDays);`
Upvotes: 0