开发者

Adding a week to Date in JavaScript: rounding error or daylight savings?

I am trying to add seven days to a Data object, however at some stage I start getting strange results.

var currDate = new Date(2011, 2, 28)
  , oldTicks = currDate.getTime()
  , newTicks = oldTicks + (86400000 * 7)
  , nextWeek = new Date(newTicks)
console.log('Old ticks: ' + oldTicks)
console.log('New ticks: ' + newTicks)
console.log('New date : ' + nextWeek)

The output I get, both Chrome/FF is:

Old ticks: 1301230800000
New ticks: 1301835600000
log: New date : Sun Apr 03 2011 23:00:00 GMT+1000 (EST)

Expected to get:

lo开发者_如何学Gog: New date : Mon Apr 04 2011 23:00:00 GMT+1000 (EST)

As you can see, instead of adding 7 days, just 6 were added. The code above, however, works fine with other dates, say 2011 Apr 28 or 2011 May 28.


Crescent Fresh is correct form what I can deduce. Looking up timezones GMT+1000 (EST) looks like Australia Eastern Standard Time - from wikipedia - list of timezones by UTC offset

And from wikipedia - daylist savings time around the world, shows that Australia switches from standard to daylight savings time in between the date ranges specified by the OP.


If it were me I'd do:

var curDate = new Date(),

var aWeekLater = new Date(curDate.getFullYear(), curDate.getMonth(), curDate.getDate() + 7);

with some possible adjustments for time of day.

That said, when I try your code in my Chrome developer console, I get 04 Apr as the answer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜