0

I am getting an error when I try to format the following date.

var d = new Date('Jan 01 2019 12:00AM');

I am getting Invalid Date

user3525290
  • 1,557
  • 2
  • 20
  • 47
  • The documentation for [`Date.parse`](https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Date/parse) (which is implicitely called in `new Date(dateString)`) tells us that the string should have [RCF2822](https://tools.ietf.org/html/rfc2822#section-3.3) format. You can probably judge from the examples on MDN what the problem is (hint: `new Date('01 Jan 2019 12:00 GMT')` works). – FK82 Jan 02 '19 at 21:58
  • @FK82—'01 Jan 2019 12:00 GMT' is not a format supported by ECMAScript, so parsing is implementation dependent. There is no reference to RCF2822 in ECMA-262, however there is a detailed explanation of the format to be produced by [*Date.prototype.toString*](http://ecma-international.org/ecma-262/9.0/#sec-date.prototype.tostring), which should also be parsed correctly (in implementations consistent with ECMAScript 2018). – RobG Jan 02 '19 at 22:58
  • @RobG Yeah, you're right. The ECMAScript specification actually [refers to ISO 8601 in section 20.3.1.16](https://www.ecma-international.org/ecma-262/6.0/#sec-date-time-string-format). However, [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse) states RCF 2822 or ISO 8601 format. That's a bit misleading. – FK82 Jan 03 '19 at 10:56
  • @FK82—yes, but MDN is not normative, it's a community wiki that anyone can contribute to (though many seem to treat it as some form of official documentation). The format for *toISOString* is ["based on … ISO 8601"](http://ecma-international.org/ecma-262/9.0/#sec-date-time-string-format), but is slightly modified for reasons known only to the TC39 members who made the decision. – RobG Jan 03 '19 at 22:19

1 Answers1

-1

You need to remove AM from your line new date.

var d = new Date('Jan 01 2019 12:00');

Ar2zee
  • 410
  • 1
  • 4
  • 12
  • 3
    That format is not standard and will not be recognized in all JavaScript platforms. – Pointy Jan 02 '19 at 21:57
  • Also, removing the AM shifts the time by 12 hours. 12:00AM is typically understood to be midnight, whereas 12:00 typically represents noon. – RobG Jan 03 '19 at 22:22