var today = new Date(); var thisYear = today.getFullYear(); var thisMonth = today.getMonth(); function calendar(year, month, today){ if(thisYear === year && thisMonth === month){ todayFlag = true; } else { todayFlag = false; } var startDate = new Date(year, month, 1); // その月の最初の日の情報 var endDate = new Date(year, month + 1 , 0); // その月の最後の日の情報 var startDay = startDate.getDay();// その月の最初の日の曜日を取得 var endDay = endDate.getDate();// その月の最後の日の曜日を取得 var textSkip = true; // 日にちを埋める用のフラグ var textDate = 1; // 日付(これがカウントアップされます) var tableBody =''; // テーブルのHTMLを格納する変数 for (var row = 0; row < 6; row++){ var tr = ''; for (var col = 0; col < 7; col++) { if (row === 0 && startDay === col){ textSkip = false; } if (textDate > endDay) { textSkip = true; } var addClass; if(todayFlag = true && textDate === today.getDate()){ addClass = "todayis"; } else { addClass = ""; } var textTd; if(textSkip == true){ textTd = ''; } else { textTd = textDate++; } var td = '' + textTd + ''; tr += td; } tr += ''; tableBody += tr; } $('#js-calendar-body').html(tableBody); } function head(year, month){ $('#js-year').text(year); $('#js-month').text(month + 1); } head(thisYear, thisMonth); calendar(thisYear, thisMonth, today);