Get the given weekday in the given month using JavaScript

I am trying to get the nth day of the week - for example, the second Sunday of the month in which a certain date falls.

For example, if the date is August 24, 2015, I would like to do this:

nthDayOfMonth(0, 2, new Date(2015, 7, 24))

and receive on August 9, 2015 (2nd Sunday of August). Then I would like to add a month to the date, call the function again and get September 13, 2015 (2nd Sunday of September).

For some reason, the code below does not work.

What am I missing?

function nthDayOfMonth(day, n, date) {                                                                                              
    console.log(day);
    console.log(n);
    var count = 0; 
    var idate = new Date(date);                                                                                                       

    idate.setDate(1);                                                                                                                 

    while ((count) < n) {                                                                                                             
      idate.setDate(idate.getDate() + 1);
      if (idate.getDay() == day) {
        count++;                                                                                                                      
      }                                                                                                                               
    }                                                                                                                                 

return idate;                                                                                                                     

}

+4
source share
3 answers

idate.getDay(), . , .

.

function print(s) {
  document.write(s + '<br />');
}

function nthWeekdayOfMonth(weekday, n, date) {
  var count = 0,
      idate = new Date(date.getFullYear(), date.getMonth(), 1);
  while (true) {
    if (idate.getDay() === weekday) {
      if (++count == n) {
        break;
      }
    }
    idate.setDate(idate.getDate() + 1);
  }
  return idate;
}

             // Second Sunday of the current month.
var date = new Date();
print(date = nthWeekdayOfMonth(0, 2, date)); 
             // Second Sunday of next month.
date.setMonth(date.getMonth() + 1);
print(date = nthWeekdayOfMonth(0, 2, date));

             // First Tuesday of September 2015.
print(nthWeekdayOfMonth(2, 1, new Date(2015, 8)));
             // First Wednesday of September 2015.
print(nthWeekdayOfMonth(3, 1, new Date(2015, 8)));
             // Second Tuesday of September 2015.
print(nthWeekdayOfMonth(2, 2, new Date(2015, 8)));
             // Second Wednesday of September 2015.
print(nthWeekdayOfMonth(3, 2, new Date(2015, 8)));
body {
  font-family: sans-serif;
}

, . . , , JavaScript 6, , 0.

, :

0 - 6 + 7

1. ? 0 - 6 - 6 0, , 7.

, a b

(b - a + 7) % 7

, , . . , 7 . , n, n == 1 , (n - 1) * 7 .

, date - , n - weekday,

(weekday - date.getDay() + 7) % 7 + (n - 1) * 7

.

.

function print(s) {
  document.write(s + '<br />');
}

function nthWeekdayOfMonth(weekday, n, date) {
  var date = new Date(date.getFullYear(), date.getMonth(), 1),
      add = (weekday - date.getDay() + 7) % 7 + (n - 1) * 7;
  date.setDate(1 + add);
  return date;
}

             // Second Sunday of the current month.
var date = new Date();
print(date = nthWeekdayOfMonth(0, 2, date)); 
             // Second Sunday of next month.
date.setMonth(date.getMonth() + 1);
print(date = nthWeekdayOfMonth(0, 2, date));

             // First Tuesday of September 2015.
print(nthWeekdayOfMonth(2, 1, new Date(2015, 8)));
             // First Wednesday of September 2015.
print(nthWeekdayOfMonth(3, 1, new Date(2015, 8)));
             // Second Tuesday of September 2015.
print(nthWeekdayOfMonth(2, 2, new Date(2015, 8)));
             // Second Wednesday of September 2015.
print(nthWeekdayOfMonth(3, 2, new Date(2015, 8)));
body {
  font-family: sans-serif;
}
+5

. , d.setMonth(d.getMonth()+1).

:

function nthDayOfMonth(day, n, date) {
	var count = 0,
	    idate = new Date(date);
	idate.setDate(1);

	while (count < n) {
		idate.setDate(idate.getDate() + 1);
		if (idate.getDay() == day) { count++; }
	}

	return idate;
}

// Today : 2015-08-24
var today = new Date();

// Second Sunday of current month : 2015-08-09
var res = nthDayOfMonth(0, 2, today);

// res plus 1 month : 2015-09-09 (Wednesday)
var plusOne = new Date( res );
plusOne.setMonth(plusOne.getMonth() + 1);

// Second Sunday of next month : 2015-09-13
var res2 = nthDayOfMonth(0, 2, plusOne);

document.body.innerHTML = 'Today is <br>' + today + '<br>'
                        + 'Second Sunday of current month is <br>'  + res + '<br>'
                        + 'If you add a month to it, you get <br>'  + plusOne + '<br>'
                        + 'And second Sunday of that month is <br>' + res2;
+1

I simply added the setting to Michael Laszlo's excellent answer to allow the caller to provide n == 5 (or any larger value) to indicate that the last day of the week of the month is needed:

function nthWeekdayOfMonth(weekday, n, date) {
  var month = date.getMonth();
  var date = new Date(date.getFullYear(), month, 1),
      add = (weekday - date.getDay() + 7) % 7 + (n - 1) * 7;

  // make sure that we stay in the same month
  do {
    date.setMonth(month);
    date.setDate(1 + add);
    add -= 7;
  } while (date.getMonth() != month);

  return date;
}
0
source

All Articles