Reputation: 2210
I have this piece of code in javascript to calculate the amount of seconds between 2 dates:
function dateDiffInSeconds(a, b) {
var utc1 = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate(), a.getHours(), a.getMinutes(), a.getSeconds());
var utc2 = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate(), b.getHours(), b.getMinutes(), b.getSeconds());
return Math.floor((utc2 - utc1) / 1000);
}
I have made this little setInterval
function to make it update every second and show the number on the screen:
var j = new Date('July, 05, 2012');
setInterval(function () {
var n = new Date();
document.getElementById("a").innerHTML = dateDiffInSeconds(j, n);
}, 1000);
This works great and there's no problem with that.
Now i have tried with the same piece of code to calculate every minute in that seconds, so i thought:
On every 60 seconds, count up the minutes
variable
by 1;
So i made it to this:
var j = new Date('July, 05, 2012');
var minutes = 0;
setInterval(function () {
var n = new Date();
document.getElementById("a").innerHTML = dateDiffInSeconds(j, n);
if(dateDiffInSeconds(j, n) > 60) {
minutes++;
console.log(mins);
}
}, 1000);
Problem
When i run this the minutes variable is counting up, but only on every count by 1, and not by each 60 seconds.
Wanted result
How do i make it so that on each 60 seconds calculated with the dateDiffInSeconds
function , its counting up the minutes
variable by 1
?
Upvotes: 0
Views: 75
Reputation: 9294
Here check out this answer:
Minutes: <span id="minutes"></span>
Seconds: <span id="seconds"></span>
function dateDiffInSeconds(a, b) {
var utc1 = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate(), a.getHours(), a.getMinutes(), a.getSeconds());
var utc2 = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate(), b.getHours(), b.getMinutes(), b.getSeconds());
return Math.floor((utc2 - utc1) / (1000));
}
var j = new Date();
var mins = 0;
document.getElementById("minutes").innerHTML = mins;
setInterval(function () {
var n = new Date();
document.getElementById("seconds").innerHTML = dateDiffInSeconds(j, n);
if(dateDiffInSeconds(j, n) > 60) {
mins++;
document.getElementById("minutes").innerHTML = mins;
j = new Date();
}
}, 1000)
;
Upvotes: 0
Reputation: 5781
You don't really need to "count up" the minutes variable, you can calculate it using the diff in seconds. If you want to detect when the number of minutes increases, you can just compare the newly calculated minutes with the value you calculated last time, and if they differ you can take action:
function dateDiffInSeconds(a, b) {
return Math.floor((b.getTime() - a.getTime()) / 1000);
}
var j = new Date('July, 05, 2012');
var oldDiffInMinutes = null;
setInterval(function () {
var n = new Date();
var diffInSeconds = dateDiffInSeconds(j, n);
var diffInMinutes = Math.floor(diffInSeconds/60);
document.getElementById("a").innerHTML = diffInSeconds;
if (diffInMinutes !== oldDiffInMinutes) {
console.log("Minutes changed: " + diffInMinutes);
oldDiffInMinutes = diffInMinutes;
}
}, 1000);
Upvotes: 1
Reputation: 4669
You need to use a secondary counter inside your loop...
Psuedo code for your loop:
secs = 1000
minutes = 0
minuteCounter = 0
for (xx=0;xx<secs;xx++){
minuteCounter++
if (minuteCounter==60){
//We've reached one minute
minutes++
minuteCounter=0
}
}
This will count up the minutes by adding one every time the counter reaches 60.
Upvotes: 1