1

I'm confused because typing the same date in a different format results in two different date outputs, the first converted and the second, not. Here is the code:

var x = new Date("2015-03-25"); // outputs Tue Mar 24 2015 17:00:00 GMT-0700 (PDT)
var y = new Date("03/25/2015"); // outputs Wed Mar 25 2015 00:00:00 GMT-0700 (PDT)
Govind Rai
  • 14,406
  • 9
  • 72
  • 83
  • 1
    they're both converted (or parsed) - but the first seems to be parsed as UTC (GMT) midnight and the second as local midnight - browsers and dates in javascript are a minefield of incompatibility – Jaromanda X Oct 27 '16 at 22:07
  • Look here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date – RigidBody Oct 27 '16 at 22:08
  • 1
    `browsers and dates in javascript are a minefield of incompatibility` it's true for _any_ language I've used. Dates are easily my least favourite thing in the entirety of programming. I'll even take phantom problems that only manifest if you're not debugging over dates. – VLAZ Oct 27 '16 at 22:11

1 Answers1

3

The way dates are parsed by browsers is a huge pile of unpredictable inconsistency. You should not attempt it. Here's the full rundown in case you're curious: http://dygraphs.com/date-formats.html

If you want consistent parsing you should implement it yourself or us a library that does it. Momentjs is widely used: http://momentjs.com/

jods
  • 4,581
  • 16
  • 20