1

Try this:

date = new Date(2012,9,20)
Sat Oct 20 2012 00:00:00 GMT-0300 (BRT)
new Date(date.getFullYear(), date.getMonth(), date.getDate()+1)
Sat Oct 20 2012 23:00:00 GMT-0300 (BRT)

(tested on Chrome and Firebug)

But this works:

date = new Date(2012,10,20)
Sat Nov 20 2012 00:00:00 GMT-0300 (BRT)
new Date(date.getFullYear(), date.getMonth(), date.getDate()+1)
Sat Nov 21 2012 0:00:00 GMT-0300 (BRT)
PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
brauliobo
  • 5,843
  • 4
  • 29
  • 34

3 Answers3

5

The problem is that daylight saving's time started on Oct 20 in Brazil (BRT). Try using UTC time:

date= new Date(Date.UTC(2012,9,20)); // zero-based month: 9->october
new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()+1))

This should make your date advancement independent of daylight saving's time, allowing for your transition to happen smoothly. For more information, see Javascript dates: what is the best way to deal with Daylight Savings Time?

Alternatively, you could set your date's time to something in the middle of the day, like, say, noon, if all you really care about is the day.

date = new Date(2012,9,20,12)
new Date(date.getFullYear(), date.getMonth(), date.getDate()+1)
Community
  • 1
  • 1
PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
  • 1
    This is not enough, you must use `date.getUTCFullYear()`, `date.getUTCMonth()()` and `date.getUTCDate()` – Esailija Dec 04 '12 at 13:56
  • 2
    also, `date` is a number, needs `date = new Date( Date.UTC( ..` :P – Esailija Dec 04 '12 at 13:58
  • It needs the `new Date( Date.UTC(` again on line 2 and I'll give +1 – Paul S. Dec 04 '12 at 14:01
  • That's what I get for not really testing it out... Anyways, I've made that last correction as well. – PearsonArtPhoto Dec 04 '12 at 14:03
  • You got my +1.. but to think you could've done all that with just [`date.setUTCDate( date.getUTCDate() + 1 )`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/setUTCDate) – Paul S. Dec 04 '12 at 14:06
  • not yet... date= new Date(Date.UTC(2012,9,20)); // zero-based month: 9->october Fri Oct 19 2012 21:00:00 GMT-0300 (BRT) new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()+1)) Sat Oct 20 2012 21:00:00 GMT-0300 (BRT) – brauliobo Dec 04 '12 at 14:51
  • need to get that time on my locale: so the expected result would be like in november 20 – brauliobo Dec 04 '12 at 14:54
  • Why does your locale matter? – PearsonArtPhoto Dec 04 '12 at 16:47
1

It is daylight saving time: at this time in GMT-0300 time is rewinded 1 hour backwards. Your sample is working fine in GMT+0400 timezone.

See question 1º Day of Daylight Saving Time Java and JS showing a different behavior

Community
  • 1
  • 1
Ivan Solntsev
  • 2,081
  • 2
  • 31
  • 40
0

It works just fine:

console.log(date = new Date(2012,9,20))
console.log(new Date(date.getFullYear(), date.getMonth(), date.getDate()+1))
// returns:
// Sat Oct 20 2012 00:00:00 GMT+0200 (West-Europa (zomertijd))
// Sun Oct 21 2012 00:00:00 GMT+0200 (West-Europa (zomertijd))

(Don't mind the localized string at the end)

Apparently you just hit the day daylight saving's time started in your localization. try Date.UTC(), instead, unless you specifically need the time for your localization.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • 1
    Yes, because your date object is set to different timezone than the OP, your answer is completely irrelevant. Note that you cannot explicitly set timezones in js, you have to actually change your system timezone to use different timezone for your date objects. – Esailija Dec 04 '12 at 13:51
  • Then the question might as well be closed as a textbook example of "Too localized" – Cerbrus Dec 04 '12 at 13:53
  • No, it's quite relevant, to anyone who happens to program in Brazil. To others, well, it might not matter so much... – PearsonArtPhoto Dec 04 '12 at 13:54
  • 1
    Well the correct answer could still be helpful for anyone: always work with UTC – Esailija Dec 04 '12 at 13:55