Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if a date is valid in javascript [duplicate]

Tags:

javascript

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

Is there is a function IsDate() in javascript?

like image 690
Guilherme Costa Avatar asked May 14 '12 19:05

Guilherme Costa


People also ask

How can I check if a date has a valid moment?

isValid() is the method available on moment which tells if the date is valid or not. MomentJS also provides many parsing flags which can be used to check for date validation.

How do you check if a date is after another date in JavaScript?

To check if a date is after another date, compare the Date objects, e.g. date1 > date2 . If the comparison returns true , then the first date is after the second, otherwise the first date is equal to or comes before the second. Copied!


1 Answers

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

like image 178
Gabriel Jürgens Avatar answered Oct 11 '22 09:10

Gabriel Jürgens