Toni Michel Caubet
Toni Michel Caubet

Reputation: 20163

Get all mondays in the year

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?

Upvotes: 5

Views: 8350

Answers (4)

Quan Tran
Quan Tran

Reputation: 186

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 })

Upvotes: 1

RobG
RobG

Reputation: 147363

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())

Upvotes: 0

Rick
Rick

Reputation: 1055

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>");

Upvotes: 0

Jose Gomez
Jose Gomez

Reputation: 628

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);
}

Upvotes: 7

Related Questions