Is there a way to check if a variable is a Date in JavaScript?
JavascriptDateTypecheckingJavascript Problem Overview
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!
Javascript Solutions
Solution 1 - Javascript
Use instanceof
(myvar instanceof Date) // returns true or false
Solution 2 - Javascript
Object.prototype.toString.call(obj) === "[object Date]"
will work in every case, and obj instanceof Date
will only work in date objects from the same view instance (window
).
Solution 3 - Javascript
if(obj && obj.getUTCDay){ // I'll treat it like a Date }
Solution 4 - Javascript
if (parseDate("datestring"))