Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to check if a variable is a Date in JavaScript? [duplicate]

I was wondering if there is any way to check if an object is specifically a Date in JavaScript. isType returns object for Date, which isn't enough for this scenario. Any ideas? Thanks!

like image 857
Kyle Hotchkiss Avatar asked May 14 '10 01:05

Kyle Hotchkiss


People also ask

How do you check if a variable is a date in JavaScript?

You can use the ! isNaN() function to check whether a date is valid. If x is a Date, isNaN(x) is equivalent to Number.

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.

How do you check if a string is a date JavaScript?

Using the Date. One way to check if a string is date string with JavaScript is to use the Date. parse method. Date. parse returns a timestamp in milliseconds if the string is a valid date.


1 Answers

Use instanceof

(myvar instanceof Date) // returns true or false 
like image 94
BrunoLM Avatar answered Sep 21 '22 13:09

BrunoLM