Quantcast
Viewing latest article 28
Browse Latest Browse All 58

Answer by Ash Clarke for Detecting an "invalid date" Date instance in JavaScript

My solution is for simply checking whether you get a valid date object:

Implementation

Date.prototype.isValid = function () {
    // An invalid date object returns NaN for getTime() and NaN is the only
    // object not strictly equal to itself.
    return this.getTime() === this.getTime();
};  

Usage

var d = new Date("lol");

console.log(d.isValid()); // false

d = new Date("2012/09/11");

console.log(d.isValid()); // true

Viewing latest article 28
Browse Latest Browse All 58

Trending Articles