Checking if a date is valid in javascript

Javascript

Javascript Problem Overview


> Possible Duplicate:
> Detecting an “invalid date” Date instance in JavaScript

Is there is a function IsDate() in javascript?

Javascript Solutions


Solution 1 - Javascript

Try this:

var date = new Date();
console.log(date instanceof Date && !isNaN(date.valueOf()));

This should return true.

UPDATED: Added isNaN check to handle the case commented by Julian H. Lam

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGuilherme CostaView Question on Stackoverflow
Solution 1 - JavascriptGabriel JürgensView Answer on Stackoverflow