Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You seem to be conflating two things here. Valid date objects and valid dates. These are not the same problem.</p> <p><a href="https://stackoverflow.com/questions/1353684/detecting-an-invalid-date-date-instance-in-javascript">The question you linked</a> to answers how to test for validity of date <strong>objects</strong> (whether a date object is an "invalid date" instance). Invalid date objects are generated when you use invalid parameters when constructing them: <code>new Date('?')</code></p> <p>What you want is to test if a date string conforms to a predefined date format. This is an entirely different problem that <em>should not be solved by using only date objects.</em></p> <p>Generally speaking, there are a couple of reasons for this; the first is that the browsers will helpfully compute overflow months/days/time to the correct date: <code>new Date(2012,0,290)</code> === Oct 06 2012. </p> <p>Secondly because the parser may be locale dependent (mm/dd vs. dd/mm?). When the date is parsed by the browser my locale may cause it roll it to my timezone/DST thus skewing it and messing up detection (<code>.getDate</code> may now return next day over). Even worse, this may only occur across some timezones at certain parts of the year.</p> <p><strong>I strongly encourage using a library</strong> like <a href="http://www.datejs.com/" rel="nofollow noreferrer">date.js</a> to handle this stuff because <a href="http://infiniteundo.com/post/25326999628/falsehoods-programmers-believe-about-time" rel="nofollow noreferrer">dates are much harder than you think</a>! If you absolutely must validate by hand, then I recommend doing it in detail like this:</p> <pre><code>function isValidDate ( str ) { // parse to numbers var rm = str.split( '/' ) , m = 1 * rm[0] , d = 1 * rm[1] , y = 1 * rm[2] ; if ( isNaN( m * d * y ) ) { return false; } if ( d &lt; 1 ) { return false; } // day can't be 0 if ( m &lt; 1 || m &gt; 12 ) { return false; } // month must be 1-12 if ( m === 2 ) { // february var is_leap_year = ((y % 4 === 0) &amp;&amp; (y % 100 !== 0)) || (y % 400 === 0); if ( is_leap_year &amp;&amp; d &gt; 29 ) { return false; } // leap year if ( !is_leap_year &amp;&amp; d &gt; 28 ) { return false; } // non-leap year } // test any other month else if ((( m === 4 || m === 6 || m === 9 || m === 11 ) &amp;&amp; d &gt; 30) || (( m === 1 || m === 3 || m === 5 || m === 7 || m === 8 || m === 10 || m === 12 ) &amp;&amp; d &gt; 31)) { return false; } return true; } </code></pre> <p>As a jsFiddle: <a href="http://jsfiddle.net/3pMPp/1/" rel="nofollow noreferrer">http://jsfiddle.net/3pMPp/1/</a><br> As a jsPerf: <a href="http://jsperf.com/silly-date-valiation" rel="nofollow noreferrer">http://jsperf.com/silly-date-valiation</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload