Quantcast
Channel: Detecting an "invalid date" Date instance in JavaScript - Stack Overflow
Viewing all articles
Browse latest Browse all 58

Answer by Steven Spungin for Detecting an "invalid date" Date instance in JavaScript

$
0
0

After reading every answer so far, I am going to ofter the most simple of answers.

Every solution here mentions calling date.getTime(). However, this is not needed, as the default conversion from Date to Number is to use the getTime() value. Yep, your type checking will complain. :) And the OP cleary knows they have a Date object, so no need to test for that either.

To test for an invalid date:

isNaN(date)

To test for a valid date:

!isNaN(date)

Viewing all articles
Browse latest Browse all 58

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>