Javascript Setting Time Difference Through Loop In Array
Solution 1:
Hi i have created a jsfiddle example:-
var setIntervals = function (start, end, inc) {
start = start.toString().split(':');
end = end.toString().split(':');
inc = parseInt(inc, 10);
var pad = function (n) { return (n < 10) ? '0' + n.toString() : n; },
startHr = parseInt(start[0], 10),
startMin = parseInt(start[1], 10),
endHr = parseInt(end[0], 10),
endMin = parseInt(end[1], 10),
currentHr = startHr,
currentMin = startMin,
previous = currentHr + ':' + pad(currentMin),
current = '',
r = [];
do {
currentMin += inc;
if ((currentMin % 60) === 0 || currentMin > 60) {
currentMin = (currentMin === 60) ? 0 : currentMin - 60;
currentHr += 1;
}
current = currentHr + ':' + pad(currentMin);
r.push(previous + ' - ' + current);
previous = current;
} while (currentHr !== endHr);
return r;
};
click here to see example:-http://jsfiddle.net/w6EQ6/3/
or if you don't want to print the range then see the below given link:-
fixed the issue:http://jsfiddle.net/w6EQ6/8/
Solution 2:
Well , if you need you to make your loop stop in 10:00 here is the code
vararray = newArray();
var timeDiff = 15;
var end = 10;
var start = 9;
for (var xh = start; xh <= end; xh++) {
for (var xm = 0; xm < 60; xm += timeDiff) {
array.push(("0" + xh).slice(-2) + ':' + ("0" + xm).slice(-2));
if(xh==end)
break;
}
};
now if you want to calculate the difference between two dates here is the function
functiondifference(date1,date2)
{
date1 = newDate(0,0,0,parseInt(date1.split(":")[0]),parseInt(date1.split(":")[1]));
date2 = newDate(0,0,0,parseInt(date2.split(":")[0]),parseInt(date2.split(":")[1]));
diff = (((date1>date2)?1:-1)*(date1-date2))/(1000*60*60);
return diff;
}
you can use this function like this
var diff = difference(array[0],array[1]); //for example
This function returns the difference in hours if you need in minutes or secondes you have to change the diff variable like this
// in minutes
diff = (((date1>date2)?1:-1)*(date1-date2))/(1000*60);
return diff;
// in seconds
diff = (((date1>date2)?1:-1)*(date1-date2))/1000;
return diff;
Here is a jsFiddle
Solution 3:
Here is an answer that works for 9:30 to 10:30 using one of the answers here:
functionpadLeft(value, padding) {
return String(padding + value).slice(-padding.length);
}
functiongetTimeInterval(start, end, interval) {
var result = [];
var date = new Date();
date.setHours(start.hour);
date.setMinutes(start.minute);
while (!(date.getHours() === end.hour && date.getMinutes() === end.minute + interval)) {
result.push(padLeft(date.getHours(), '00') + ':' + padLeft(date.getMinutes(), '00'));
date = new Date(date.getTime() + interval * 60 * 1000);
}
return result;
}
console.log(getTimeInterval({
hour: 9,
minute: 30
}, {
hour: 10,
minute: 30
}, 15));
Solution 4:
You are looping one extra, below code will solve your problem
for (var xh = FirstTime; xh < endTime; xh++) {
for (var xm = 0; xm < 60; xm += timeDiff) {
array.push(("0" + xh).slice(-2) + ':' + ("0" + xm).slice(-2));
}
};
array.push(("0" + xh).slice(-2)+':' + ("00").slice(-2));
and updated jsfiddle
Solution 5:
The problem is with your nesting of for loops. What happens in you code is that the inner loop is executed twice. Once while xh=9 and once when xh=10. This is why you are getting more times than you expect.
One fix would be to change
xh <= endTime
to
xh < endTime
This would result in every time up to endTime being printed. To make it inclusive, I would add another line to add the endTime to the array.
So your final loop might look like
for (var xh = FirstTime; xh < endTime; xh++) {
for (var xm = 0; xm < 60; xm += timeDiff) {
array.push(("0" + xh).slice(-2) + ':' + ("0" + xm).slice(-2));
}
array.push(("0" + endTime).slice(-2) + ':' + ("00").slice(-2));
};
Post a Comment for "Javascript Setting Time Difference Through Loop In Array"