在 JavaScript 中有没有检查变量是否为 Date 的方法?

我想知道是否有任何方法可以检查对象是否是 JavaScript 中的 Date。IsType 为 Date 返回对象,这对于本场景来说是不够的。有什么想法吗?谢谢!

76689 次浏览

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