Eason Eason - 24 days ago 5
Javascript Question

Strange Date issue for javascript

I try to troubleshot my code and found a strange Date issue for js, I can't understand.
On the developer tools of chrome, watch the value of this expression:

new Date(new Date() + (-1) * 60 * 1000).getTime()


You will got value 'NaN'.

And if watch this:

new Date(new Date() + (-1) * 60 * 100).getTime()


You will got value '1479095577000'.

Why?

Answer
  • new Date() + (-1) - the date get converted to string, then "-1" is concatenated to it, which is a valid date, because you're subtracting some time zone offset
  • new Date() + (-1) * 60 * 100 = new Date() + (-1 * 60 * 100) = new Date() + (-6000) - which is also a valid date minus some time zone shifting
  • but then a date minus 60000 is not a valid date, due to time zones having only 4 digits top
Comments