开发者

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

开发者 https://www.devze.com 2022-12-30 01:17 出处:网络
This question already has answers here: How to check whether an object is a date? (26 answers) Closed 8 years ago.
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

精彩评论

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

关注公众号