 <p>One-liner direct computation (no Date object):</p> <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code"> <pre class="snippet-code-js lang-js prettyprint-override"><code>function daysInMonth(m, y) {//m is 1-based, feb = 2 return 31 - (--m ^ 1? m % 7 &amp; 1: y &amp; 3? 3: y % 25? 2: y &amp; 15? 3: 2); } console.log(daysInMonth(2, 1999)); // February in a non-leap year console.log(daysInMonth(2, 2000)); // February in a leap year</code></pre> </div> </div> </p> <p>Variation with 0-based months:</p> <pre><code>function daysInMonth(m, y) {//m is 0-based, feb = 1 return 31 - (m ^ 1? m % 7 &amp; 1: y &amp; 3? 3: y % 25? 2: y &amp; 15? 3: 2); } </code></pre>