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!
Use instanceof
(myvar instanceof Date) // returns true or false
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
).
if(obj && obj.getUTCDay){ // I'll treat it like a Date }
if (parseDate("datestring"))
精彩评论