8

When I'm creating a new Date object and pass in a date using hyphens

new Date("2015-07-02") // I get Thu Jul 02 2015 01:00:00 GMT+0100 (IST)

and when I use forward slashes

new Date("2015/07/02") // I get Thu Jul 02 2015 00:00:00 GMT+0100 (IST)

notice the time difference: 01:00:00 for hyphens and 00:00:00 for forward slashes

this breaks my code :(

Why this is happening? Any workaround for this? (Should I just set time to 00:00:00 when using hyphens?)

I need to be able to compare dates that have forward-slashes with dates that have hyphens and I'm not sure I might need to compare dates with some other symbols.

Is this happening to hyphens only?

Thanks.

Eugene
  • 343
  • 1
  • 3
  • 14

1 Answers1

9

If a recent browser can interpret the date string as ISO-8601 - it will do it.

examples :

 YYYY (eg 1997)
 YYYY-MM (eg 1997-07)
 YYYY-MM-DD (eg 1997-07-16)
 YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
 YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
 YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)

With this format, your date/time string is interpreted as UTC(!!!).

You should Stick to "YYYY-MM-DD" for your date strings whenever possible

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • 3
    Having it interpreted as UTC is not a bad thing, is it? UTC should be used as a standard anyway, so you should stick to the ISO standard `YYYY-MM-DD` whenever possible. – Bergi Apr 09 '14 at 10:57