I have a Javascript Date object equal to 00:30 and when doing:
date.setMinutes(date.getMinutes() + 30);
causes the date object to equal 00:00.
Does anyone know why this is happening?
Here is where the code is being used:
for (var i = openTime; i <= closeTime; i.setMinutes(i.getMinutes() + timeIncrement)) {
var time = i.getHours() + (i.getHours() == 0 ? '0' : '') + ':' + i.getMinutes() + (i.getMinutes() == 3 || i.getMinutes() == 0 ? '0' : '');
$(timeClientId).append($('<option />').val(time).text(time));
}
The above script creates a list of times available from 10:00am all the way to 02:00am the next day.
It runs fine until it reaches midnight 00:00 after many successful iterations.
Can anyone help?
Thanks!
ANSWER/SOLUTION:
This problem was due to a daylight saving issue, so this Saturday the clocks go forward. For some odd reason when adding 30 minutes to 12:30 it reset back to 12:00 using .setMinutes(). This kept it in an endless loop.
The solution was to add minutes using
i.setTime(i.getTime() + timeIncrement * 60 * 1000)
This sorted the issue.
Cheers for all your answers!
You are only setting the minutes. So of course 30 minutes + 30 minutes on the clock equals 60 minutes, i.e. 0 minutes.
Use this clever method (it’s clever because it works with all rollovers!):