0

I'm trying to instance a Date in this way

var date = new Date('20/08/1990');

but its returning Invalid Date.

How can a I do it?

Alan
  • 2,559
  • 4
  • 32
  • 53
  • works for me, also with surprising result (`1991-08-08T04:00:00.000Z`). how do you know this is the format your js interpreter expects anyway? – njzk2 Jan 07 '16 at 19:38
  • the way you are initiating the date object seems not to be correct. Please check the valid initialization formats in mdn site - https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date – Pradeep Potnuru Jan 07 '16 at 19:41
  • You're effectively invoking the [`Date.parse` method](http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.4.2), where it will first see if it is [this format](http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.1.15), which it isn't, so: _"If the String does not conform to that format the function may fall back to any implementation-specific heuristics or implementation-specific date formats."_ So essentially what happens with your line of code is completely up to the JS implementation you're running inside, and may vary from browser to browser. – James Thorpe Jan 07 '16 at 19:44
  • If you're doing anything extensive with date objects in Javascript, I highly recommend the moment.js library: http://momentjs.com/ Unfortunately the core functionality for dates and times is pretty limited and inconsistent. – Julian Jan 07 '16 at 19:44

1 Answers1

0

When passing a string to Date constructor you should use English notation for dates, i.e YYYY/MM/DD.

That means you should be passing new Date('1990/08/20');

Documentation about Date class here.

Cristiano Mendonça
  • 1,220
  • 1
  • 10
  • 21