1

I would like to limit users to selecting only the first and third Monday of each month. We have a volunteer intake only on these days, so I want to limit incorrect date selections as much as possible.

I'm not a js coder, but have managed to adapt some code I found online to allow the first or third Monday of each month, but I can't work out how to allow both of them.

Here's the code I have for the first Monday:

var firstMonday = new Date(date);
var mondays=0;
firstMonday.setDate(1);

while (mondays < 1) {
    firstMonday.setDate(firstMonday.getDate() + 1);
    if (firstMonday.getDay() == 1) {
        mondays++;
    }
}
var result = date.getDate() != firstMonday.getDate();

2 Answers2

0

I think this is what you are asking. Credit to jabclab for the getMondays() function.

// test: first monday of this month
// result: true
//var dates = [new Date(2017,8,4)];

// test: third monday of this month
// result: true
//var dates = [new Date(2017,8,18)];

// test: first and third monday of this month
// result: true
var dates = [new Date(2017,8,4), new Date(2017,8,18)];

// test: first monday, third monday, and random day from this month
// result: false
//var dates = [new Date(2017,8,4), new Date(2017,8,18), new Date(2017,8,22)];

alert(validate(dates));

function validate(dates) {
  var valid = true;
  var mondays = getMondays();
  var firstMonday = mondays[0].setHours(0,0,0,0);
  var thirdMonday = mondays[2].setHours(0,0,0,0);
  
  if (dates && dates.length > 0) {
    for (var i = 0; i < dates.length; i++) {
      // Zero out time so only year, month, and day is compared
      var d = dates[i].setHours(0,0,0,0);
      
      if (d != firstMonday && d != thirdMonday) {
        return false;
      }
    }
  }
  else {
    valid = false;
  }
  
  return valid;
}

function getMondays() {
  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) {
    mondays.push(new Date(d.getTime()));
    d.setDate(d.getDate() + 7);
  }

  return mondays;
}
JAKEtheJAB
  • 289
  • 1
  • 11
0

Thanks, but I'm not sure if the above works or not as I was looking for a js code answer - I'll leave that for someone else to work out.

...which I've found in the meantime. Many thanks to Hugh at Fabrik for the following:

var thisdate = new Date(date);
thisdate.setHours(0,0,0,0);

var day = 1; // monday
var nth = 1; // first

var first = new Date(thisdate.getFullYear(), thisdate.getMonth(), 1),
  add = (day - first.getDay() + 7) % 7 + (nth - 1) * 7;
first.setDate(1 + add);

nth = 3; // third

var third = new Date(thisdate.getFullYear(), thisdate.getMonth(), 1),
  add = (day - third.getDay() + 7) % 7 + (nth - 1) * 7;
third.setDate(1 + add);

//console.log(thisdate + ', ' + first + ', ' + third);

var result = (first.getTime() !== thisdate.getTime()) && (third.getTime() !== thisdate.getTime());