3

I always have problems figuring out dates functions

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

d.setDate(1);

// Get the first Monday in the month
while (d.getDay() !== 1) {
    d.setDate(d.getDate() + 1);
}

// Get all the other Mondays in the month
while (d.getMonth() === month) {
    var pushDate = new Date(d.getTime());
    mondays.push(pushDate.getDate() + '-' + (pushDate.getMonth()+1) + '-' + pushDate.getFullYear());
    d.setDate(d.getDate() + 7);
}

I am using this function to get all mondays in a month, current month.

How can I adapt this code to get all remaining mondays in the year?

Toni Michel Caubet
  • 19,333
  • 56
  • 202
  • 378

4 Answers4

5

Just loop through the year instead of the month. The code is the same as yours, it works fine. just changed month -> year and getMonth() -> getYear()

var d = new Date(),
    year = d.getYear(),
    mondays = [];

d.setDate(1);

// Get the first Monday in the month
while (d.getDay() !== 1) {
    d.setDate(d.getDate() + 1);
}

// Get all the other Mondays in the month
while (d.getYear() === year) {
    var pushDate = new Date(d.getTime());
    mondays.push(pushDate.getDate() + '-' + (pushDate.getMonth()+1) + '-' + pushDate.getFullYear());
    d.setDate(d.getDate() + 7);
}
Jose Gomez
  • 598
  • 4
  • 14
1

With date-fns https://date-fns.org/v2.28.0/docs/eachWeekOfInterval

eachWeekOfInterval({
    start: new Date('2022/01/01'),
    end: new Date('2022/5/31')
}, { weekStartsOn: 1 })
Quan Tran
  • 146
  • 2
  • 6
0

var x = new Date();
//set the financial year starting date
x.setFullYear(2016, 03, 01);
//set the next financial year starting date
var y = new Date();
y.setFullYear(2017, 03, 01);
var j = 1;
var count = 0;
//getting the all mondays in a financial year
for (var i = 0; x < y; i += j) {
  if (x.getDay() === 1) {
    document.write("Date : " + x.getDate() + "/" +
      (x.getMonth() + 1) + "<br>");
    x = new Date(x.getTime() + (7 * 24 * 60 * 60 * 1000));
    j = 7;
    count++;
  } else {
    j = 1;
    x = new Date(x.getTime() + (24 * 60 * 60 * 1000));
  }
}
document.write("total mondays : " + count + "<br>");
Rick
  • 1,035
  • 10
  • 18
  • Please don't add days by adding 24 hours. In places where daylight saving is observed, not all days are 24 hours long. See [*Add +1 to current date*](https://stackoverflow.com/questions/9989382/add-1-to-current-date). – RobG Jul 03 '17 at 20:37
0

This is just an alternative, it uses a simpler method of getting the first day of the month.

// Get all Mondays in year from provided date
// Default today's date
function getMondays(d) {
  // Copy d if provided
  d = d ? new Date(+d) : new Date();
  // Set to start of month
  d.setDate(1);
  // Store end year and month
  var endYear = d.getFullYear() + 1;
  var endMonth = d.getMonth();

  // Set to first Monday
  d.setDate(d.getDate() + (8 - (d.getDay() || 7)) % 7);
  var mondays = [new Date(+d)];

  // Create Dates for all Mondays up to end year and month
  while (d.getFullYear() < endYear || d.getMonth() != endMonth) {
    mondays.push(new Date(d.setDate(d.getDate() + 7)));
  }
  return mondays;
}

// Get all Mondays and display result
// SO console doensn't show all results
var mondays = getMondays();
mondays.forEach(function(mon) {
  console.log(mon.toLocaleString(void 0, {
    weekday: 'short',
    day: 'numeric',
    month: 'short',
    year: 'numeric'
  }));
});

// Count of Mondays, not all shown in SO console
console.log('There are ' + mondays.length + ' Mondays, the first is ' + mondays[0].toString())
RobG
  • 142,382
  • 31
  • 172
  • 209