11

I wrote some code in Javascript that was working without any problem. But when I put the date October 20, 2013, I was returned the date October 19, 2013.

The same applies to the years 2019, 2024 and 2030 (not tested previous years and not later).

This problem shows up in all browsers I test (Google Chrome, Internet Explorer, Mozilla Firefox, Opera and Safari).

When I write:

date = new Date("10/20/2013");
document.write(date);

The result I get is:

Sat Oct 19 2013 23:00:00 GMT-0300 (BRT)


Someone could tell me why this is happening and how can I solve this problem?

3
  • Works fine for me in newest Chrome, outputs Oct 20, as expected? -> Fiddle
    – adeneo
    Commented May 22, 2013 at 18:09
  • 2
    Maybe it's your timezone...
    – Luigi Siri
    Commented May 22, 2013 at 18:10
  • Here also working normally.
    – eLRuLL
    Commented May 22, 2013 at 18:13

2 Answers 2

14

October 20th, 2013 is the cutover for BRST. So if you are in Brazil (in a BRST timezone) you will transition from BRT (UTC -3) to BRST (UTC-2).

From TimeAndDate.com:

Current time in São Paulo: Wednesday, May 22, 2013 at 3:19:14 PM BRT

São Paulo will stay on BRT until Sunday, October 20, 2013 going to BRST

The transition happens at Midnight and Midnight to 1am is skipped. To guarantee a time on that date you can try:

date = new Date("10/20/2013 01:00:00");

And you should get 10/20/2013 01:00:00 with BRST as the timezone designation.

For someone in a US timezone that participates in DST who wants to see this issue; Use the US daylight savings time transition point of March 10th 2013, where 2am is skipped ahead to 3am:

var d = new Date("03/10/2013 02:59:59")
alert(d);   // Returns 1:59:59 AM in the Standard Time Zone

var d = new Date("03/10/2013 03:00:00")
alert(d);   // Returns 3:00:00 AM in the Daylight Time Zone
3
  • More information about JS issues with dates that have time changes stackoverflow.com/questions/14839244/…
    – jwl
    Commented May 22, 2013 at 18:23
  • 1
    Which explains why only on certain years, i.e when the date falls on a Sunday! Good catch John.
    – HBP
    Commented May 22, 2013 at 18:46
  • Great! Google Chrome return your Date to 1 hour but Safari mantains changing to BRST. Commented Nov 14, 2013 at 16:12
0

Some browsers may show incorrect values when using Date.parse (which is the same as 'new Date(string)'). I don't know the reason, but the best way of creating a date is using the Date(year, month, day) constructor.

Example:

console.log(new Date(2013, 10, 20))
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.