最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Adding 30 minutes to Date causes it to go back 30 minutes - Stack Overflow

programmeradmin0浏览0评论

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!

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!

Share Improve this question edited Mar 22, 2012 at 13:45 Base33 asked Mar 22, 2012 at 10:29 Base33Base33 3,2154 gold badges28 silver badges31 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

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!):

function addMinutes(inDate, inMinutes)
{
    var newdate = new Date();
    newdate.setTime(inDate.getTime() + inMinutes * 60000);
    return newdate;
}

var date = new Date();

alert(addMinutes(date,-30));​

How are you initialising your date? setMinutes seems to work as expected so perhaps there is an error in your initial value. See below for my quick n dirty test.

var date = new Date(0);
document.write(date);
document.write("<br>");
date.setMinutes(30);
document.write(date);
document.write("<br>");
date.setMinutes(date.getMinutes() + 30);
document.write(date);
document.write("<br>");

Outputs:

Thu Jan 1 00:00:00 UTC 1970
Thu Jan 1 00:30:00 UTC 1970
Thu Jan 1 01:00:00 UTC 1970

date.js has a good way to do this:

new Date().add({ minutes: 30 });

or even more fluid:

new Date().add(30).minutes();

try this:

var date = new Date(2013,2,31,1,59);
document.write(date,"<br>");
date.setMinutes(date.getMinutes()+30);
document.write(date,"<br>");
date.setMinutes(date.getMinutes() + 30);
document.write(date,"<br>");
date.setMinutes(date.getMinutes() + 30);
document.write(date,"<br>");

Sun Mar 31 2013 01:59:00 GMT+0100 (CET)
Sun Mar 31 2013 01:29:00 GMT+0100 (CET)
Sun Mar 31 2013 01:59:00 GMT+0100 (CET)
Sun Mar 31 2013 01:29:00 GMT+0100 (CET)

So obviously the switch to summer time in Germany is causing this result. But was this intended? Entering 2013,2,31,2,59 gives same result, as if the hour 1-2 did not exist. it does not look right to me.

发布评论

评论列表(0)

  1. 暂无评论