Get mondays of a month

issue when the current month is december and year will be set to next year

by ravimallya

HTML

<div></div>
<p></p>

JavaScript

var d = new Date(),
     month = d.getMonth(),
     mondays = [],
     mondaysChange = [];

 function getMondaysChange(month) {
     //empty the array
     mondaysChange.length = 0;
     //change the type of m to number
     month = +month;
     d.setDate(1);
     d.setMonth(month);
     //  console.log("you have selected: " + d + " month");
     while (d.getDay() !== 1) {
         d.setDate(d.getDate() + 1);
     }
     // Get all the other Mondays in the month
     while (d.getMonth() === month) {
         mondaysChange.push(new Date(d.getTime()).toLocaleDateString());
         d.setDate(d.getDate() + 7);
     }
     d = new Date();
     return mondaysChange;
 }

 function getMondays() {
     d.setDate(1);
     while (d.getDay() !== 1) {
         d.setDate(d.getDate() + 1);
     }
     // Get all the other Mondays in the month
     while (d.getMonth() === month) {
         mondays.push(new Date(d.getTime()).toLocaleDateString());
         d.setDate(d.getDate() + 7);
     }
    
     return mondays;
 }

$(function() {
    alert('first ' + d);
$('div').text(getMondaysChange(month));
     alert('second ' + d);
$('p').text(getMondaysChange(10)); // change month index here
});