2

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:

log: 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.

Art
  • 23,747
  • 29
  • 89
  • 101
  • 1
    See http://stackoverflow.com/questions/4109641/javascript-date-is-this-my-error-or-did-i-find-a-bug – Crescent Fresh Feb 22 '11 at 04:39
  • @Crescent Fresh - daylight savings ends 13 Mar 2011, so I don't see how that could possibly have anything to do with this. – Pointy Feb 22 '11 at 04:47
  • There's no way it can be a "rounding error" because all those numbers fit comfortably in a 32-bit integer, let alone a double-precision floating point value. – Pointy Feb 22 '11 at 04:48
  • 1
    The only countries that are at GMT+1000 are Caroline Islands, and Papua New Guinea, neither of which follow daylight savings. Can you also log `currDate` for us? – Anurag Feb 22 '11 at 04:49
  • What time zone is that? Also, what happens when you print the formatted date string from "currDate"? – Pointy Feb 22 '11 at 04:51
  • I would assume EST stands for Eastern Time zone which should be -5 not +10 from UTC – jmm Feb 22 '11 at 05:00
  • 3
    @Pointy: parts of Australia [still observe Daylight Savings](http://australia.gov.au/about-australia/our-country/time), which ends Apr 3rd. – Crescent Fresh Feb 22 '11 at 05:06
  • Ah well durr I guess maybe everybody *should* get to decide their own daylight savings schedule :-) – Pointy Feb 22 '11 at 10:49

2 Answers2

5

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.

typo.pl
  • 8,812
  • 2
  • 28
  • 29
  • I wonder why it is only applicable when adding milliseconds, but not days as in new Date(curDate.getFullYear(), curDate.getMonth(), curDate.getDate() + 7) – Art Feb 23 '11 at 05:32
  • 1
    @Art: it is applicable because you are adding milliseconds manually to the result of `getTime()`. That's just arithmetic. If you were to use the Date constructor as `new Date(curDate.getFullYear(), curDate.getMonth(), curDate.getDate(), curDate.getHours(), curDate.getMinutes(), curDate.getSeconds(), curDate.getMilliseconds()+ 86400000 * 7)`, it would also work. – Roatin Marth Feb 23 '11 at 19:21
1

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.

Pointy
  • 405,095
  • 59
  • 585
  • 614