Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>So, I just realize the problem with the other answers is that my question is wrong. What I wanted in the first place, was to check if a given day is the last monday of may (or any other month). Not how to get the last monday of may. Sorry about that.</p> <p>The best answer to my own question I believe is this one:</p> <pre><code>function lastMondayOfMay() { var d = new Date(2013, 5, 0); //last day of desired month return d.getDate() - (d.getDay() - 1);//1 here represents monday } </code></pre> <p>For other months, just create date pointing to the last day of the month you want.</p> <p>Thanks for everyone effort trying to answer this one. But my questions still remains: Which is the best way to know if a given date is the last monday of the month. Using my solution and them comparing the dates is what I'm currently doing. Is there a more direct way of doing it?</p> <p>I believe this will answer my second question as well:</p> <pre><code>function isLastMonday(date) {//date format: 'DD/MM/YYYY' var d = new Date(date); return d.getDay() === 1 &amp;&amp; (d.getDate() + 7) &gt; 30; //1 here represents monday } isLastMonday('05/27/2013');//true isLastMonday('05/26/2013');//false isLastMonday('02/25/2013');//true isLastMonday('03/24/2013');//false </code></pre> <p>I'm actually testing with a much larger number of dates and they appear to be fine. Can anyone come with a scenario where my code will fail?</p> <p>Ok, I will answer this third question as well. The only month this could happen is february. This because to fail my test, the last monday should happen before the 24rd. Since I'm only worried about may (I'm using it to calculate the Memorial Day holiday), I believe my solution will do.</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