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

JavascriptDateTypechecking

Javascript 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"))

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
QuestionKyle HotchkissView Question on Stackoverflow
Solution 1 - JavascriptBrunoLMView Answer on Stackoverflow
Solution 2 - JavascriptEli GreyView Answer on Stackoverflow
Solution 3 - JavascriptkennebecView Answer on Stackoverflow
Solution 4 - JavascriptGermán RodríguezView Answer on Stackoverflow