title: Detecting an "invalid date" Date instance in JavaScript
body: <p>I'd like to tell the difference between valid and invalid date objects in JS, but couldn't figure out how:</p>
  
  <pre><code>var d = new Date("foo");
  console.log(d.toString()); // shows 'Invalid Date'
  console.log(typeof d); // shows 'object'
  console.log(d instanceof Date); // shows 'true'
  </code></pre>
  
  <p>Any ideas for writing an <code>isValidDate</code> function?</p>
  
  <ul>
  <li>Ash recommended <code>Date.parse</code> for parsing date strings, which gives an authoritative way to check if the date string is valid.</li>
  <li>What I would prefer, if possible, is have my API accept a Date instance and to be able to check/assert whether it's valid or not. Borgar's solution does that, but I need to test it across browsers. I also wonder whether there's a more elegant way.</li>
  <li>Ash made me consider not having my API accept <code>Date</code> instances at all, this would be easiest to validate.</li>
  <li>Borgar suggested testing for a <code>Date</code> instance, and then testing for the <code>Date</code>'s time value. If the date is invalid, the time value is <code>NaN</code>. I checked with <a href="http://www.ecma-international.org/ecma-262/6.0/index.html#sec-time-values-and-time-range" rel="noreferrer">ECMA-262</a> and this behavior is in the standard, which is exactly what I'm looking for.</li>
  </ul>