Quantcast
Viewing latest article 4
Browse Latest Browse All 58

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

Date.prototype.toISOString throws RangeError (at least in Chromium and Firefox) on invalid dates. You can use it as a means of validation and may not need isValidDate as such (EAFP). Otherwise it's:

function isValidDate(d)
{
  try
  {
    d.toISOString();
    return true;
  }
  catch(ex)
  {
    return false;    
  }    
}

Viewing latest article 4
Browse Latest Browse All 58

Trending Articles