3

重複の可能性:
Javaスクリプトで2つの日付の間にある土曜と日曜の数を決定する方法

javascript を使用して月の日曜日の数を見つけたい

月と年を関数に渡しています

を使用して合計日数を計算できます

  <script type="text/javascript">
        function daysInMonth(month, year) {
            return new Date(year, month, 0).getDate();
        }
    </script>

しかし、日曜日の番号を見つける必要があります

提案してください

4

2 に答える 2

6

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date

function sundays(year, month) {

    var day, counter, date;

    day = 1;
    counter = 0;
    date = new Date(year, month, day);
    while (date.getMonth() === month) {
        if (date.getDay() === 0) { // Sun=0, Mon=1, Tue=2, etc.
            counter += 1;
        }
        day += 1;
        date = new Date(year, month, day);
    }
    return counter;
}

console.log(sundays(2012, 5));

于 2012-10-28T08:55:21.263 に答える
1
function sundaysInMonth( m, y ) {
  var days = new Date( y,m,0 ).getDate();
  var sundays = [ (8 - (new Date( m +'/01/'+ y ).getDay())) % 7 ];
  for ( var i = sundays[0] + 7; i < days; i += 7 ) {
    sundays.push( i );
  }
  return sundays;
}

alert( sundaysInMonth( 10,2012 ) ); //=> [ 7,14,21,28 ]
alert( sundaysInMonth( 10,2012 ).length ); //=> 4
于 2012-10-28T07:09:49.447 に答える