开发者

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

This question already has answers here: How to check whether an object is a date? (26 answers) Closed 8 years ago.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜