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)