Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>(<strong>Update</strong>: See the final example at the bottom, but the rest is left as background.)</p> <p>You can add a day to the <code>Date</code> instance and see if the month changes (because JavaScript's <code>Date</code> object fixes up invalid day-of-month values intelligently), e.g.:</p> <pre><code>function isLastDay(dt) { var test = new Date(dt.getTime()), month = test.getMonth(); test.setDate(test.getDate() + 1); return test.getMonth() !== month; } </code></pre> <p><a href="http://jsbin.com/emoto4" rel="nofollow noreferrer">Gratuitous live example</a></p> <p>...or as <a href="https://stackoverflow.com/users/14860/paxdiablo">paxdiablo</a> pointed out, you can check the resulting day-of-month, which is probably faster (one fewer function call) and is definitely a bit shorter:</p> <pre><code>function isLastDay(dt) { var test = new Date(dt.getTime()); test.setDate(test.getDate() + 1); return test.getDate() === 1; } </code></pre> <p><a href="http://jsbin.com/emoto4/2" rel="nofollow noreferrer">Another gratuitous live example</a></p> <p>You could embed more logic in there to avoid creating the temporary date object if you liked since it's <em>really</em> only needed in February and the rest is just a table lookup, but the advantage of both of the above is that they defer <em>all</em> date math to the JavaScript engine. Creating the object is not going to be expensive enough to worry about.</p> <hr> <p>...and finally: Since the <a href="http://www.ecma-international.org/publications/standards/Ecma-262.htm" rel="nofollow noreferrer">JavaScript specification</a> requires (Section 15.9.1.1) that a day is <em>exactly</em> 86,400,000 milliseconds long (when in reality days <a href="http://en.wikipedia.org/wiki/Day#Leap_seconds" rel="nofollow noreferrer">vary in length</a> a bit), we can make the above even shorter by adding the day as we :</p> <pre><code>function isLastDay(dt) { return new Date(dt.getTime() + 86400000).getDate() === 1; } </code></pre> <p><a href="http://jsbin.com/emoto4/4" rel="nofollow noreferrer">Final gratuitous example</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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