0

var d = Date("12:00 AM") returns as a date just fine, however when trying to create an instance of it new Date("12:00 AM") it returns 'Invalid date'.

I found a solution here, using moment.js, which works fine, but I would like know why getting 'Invalid Date' when using 'new Date()'

Thanks

fiddle

Community
  • 1
  • 1
Sammy
  • 3,059
  • 4
  • 23
  • 32
  • 6
    `Date()` (without `new`) does not take a parameter and just yields the current date as a string – Bergi Sep 21 '14 at 17:54
  • 1
    `12:00 AM` is only a time, not a date, and therefore too less information to make a valid datetime. What output would you expect? – Bergi Sep 21 '14 at 17:55

1 Answers1

2

From MDN:

Note: Note that JavaScript Date objects can only be instantiated by calling JavaScript Date as a constructor: calling it as a regular function (i.e. without the new operator) will return a string rather than a Date object; unlike other JavaScript object types, JavaScript Date objects have no literal syntax.

...

Invoking JavaScript Date as a function (i.e., without the new operator) will return a string representing the current date and time.

The first version of your call ignores the argument (and doesn't actually produce a Date).

user2357112
  • 260,549
  • 28
  • 431
  • 505