0

I have been using this for comparing dates on IE and other browser but could anyone explain me how this works differently?


Date() and Date("2014-08-05T07:47:02.051Z")

Have different result on:

new Date() and new Date("2014-08-05T07:47:02.051Z")


And is there any problem that will pop?

Edit

When I execute console.log(Date(expiryDate),Date()) It just gives the same value.

marlo
  • 6,998
  • 5
  • 28
  • 34

1 Answers1

1

The ES5 spec makes it pretty clear:

When Date is called as a function rather than as a constructor, it returns a String representing the current time .

If you want an actual Date instance you'll have to call it with the new operator. If you are happy with just a string representation of the date you don't need to bother with the new operator.

In other words, if you need to be able to call methods on your date (such as getTime or setHours) you'll need to use new Date but if you only want a string you can just use Date.

James Allardice
  • 164,175
  • 21
  • 332
  • 312
  • `console.log(Date(expiryDate),Date())` yields same value. so my code above(on question) does not really work when comparing dates strings. – marlo Aug 05 '14 at 09:04