javascript - 自定义日历显示当前日期的下六个月日历

标签 javascript jquery model-view-controller

我想显示当前日期的下六个月日历 '//这些是一周中的几天的标签 cal_days_labels = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']; //这些是人类可读的月份名称标签,按顺序排列

cal_months_labels = ['January', 'February', 'March', 'April',
                 'May', 'June', 'July', 'August', 'September',
                 'October', 'November', 'December'];
// these are the days of the week for each month, in order
cal_days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

// this is the current date
cal_current_date = new Date();

function Calendar(month, year) {
    this.month = (isNaN(month) || month == null) ? cal_current_date.getMonth() : month;
    this.year = (isNaN(year) || year == null) ? cal_current_date.getFullYear() : year;
    this.html = '';
}


Calendar.prototype.generateHTML = function () {

    // get first day of month
    var firstDay = new Date((new Date().getFullYear(), 0, 1));
    //var firstDay = new Date(this.year, this.month, 1);
    var startingDay = firstDay.getDay();

    // find number of days in month
    var monthLength = cal_days_in_month[this.month];

    // compensate for leap year
    if (this.month == 1) { // February only!
        if ((this.year % 4 == 0 && this.year % 100 != 0) || this.year % 400 == 0) {
            monthLength = 29;
        }
    }

    // do the header
    var monthName = cal_months_labels[this.month]
    var html = '<table class="calendar-table">';
    html += '<tr><th colspan="7">';
    html += monthName + "&nbsp;" + this.year;
    html += '</th></tr>';
    html += '<tr class="calendar-header">';
    for (var i = 0; i <= 6; i++) {
        html += '<td class="calendar-header-day">';
        html += cal_days_labels[i];
        html += '</td>';
    }
    html += '</tr><tr>';

    // fill in the days
    var day = 1;
    // this loop is for is weeks (rows)
    for (var i = 0; i < 9; i++) {
        // this loop is for weekdays (cells)
        for (var j = 0; j <= 6; j++) {
            html += '<td class="calendar-day">';
            if (day <= monthLength && (i > 0 || j >= startingDay)) {
                html += day;
                day++;
            }
            html += '</td>';
        }
        // stop making rows if we've run out of days
        if (day > monthLength) {
            break;
        } else {
            html += '</tr><tr>';
        }
    }
    html += '</tr></table>';

    this.html = html;
}

Calendar.prototype.getHTML = function () {
    return this.html;
}<script>
var cal = new Calendar();
cal.generateHTML();
document.write(cal.getHTML());

'

这是我的代码,但它只显示当前月份的日历,我想在不使用任何 jquery 库的情况下也显示下 6 个月的日历

最佳答案

因为你忘记循环了。这是工作代码。 fiddle

 // do the header
var html = ""
for(var cal=0; cal<6; cal++){
 var curr = new Date(this.year, (this.month+cal), 1);
 var startingDay = curr.getDay();
 console.log(startingDay);
  var monthName = cal_months_labels[this.month+cal];
  var monthLength = cal_days_in_month[this.month+cal];
  html += '<table class="calendar-table">';
  html += '<tr><th colspan="7">';
  html += monthName + "&nbsp;" + this.year;
  html += '</th></tr>';
  html += '<tr class="calendar-header">';
  for (var i = 0; i <= 6; i++) {
      html += '<td class="calendar-header-day">';
      html += cal_days_labels[i];
      html += '</td>';
  }
  html += '</tr><tr>';

  // fill in the days
  var day = 1;
  // this loop is for is weeks (rows)
  for (var i = 0; i < 9; i++) {
      // this loop is for weekdays (cells)
      for (var j = 0; j <= 6; j++) {
          html += '<td class="calendar-day">';
          if (day <= monthLength && (i > 0 || j >= startingDay)) {
              html += day;
              day++;
          }
          html += '</td>';
      }
      // stop making rows if we've run out of days
      if (day > monthLength) {
          break;
      } else {
          html += '</tr><tr>';
      }
  }
  html += '</tr></table>';

}//end of calendar loop

关于javascript - 自定义日历显示当前日期的下六个月日历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38586242/

相关文章:

json - extjs使用MVC从json文件加载树

javascript - 图表 js 标签和分组标签

javascript - $(window).resize() 或 $(window).on ('resize' ) 在窗口最大化或恢复时不会触发。这些事件还有其他触发因素吗?

javascript - 如何使用 css 按钮调用 jquery 函数?

javascript - 元素位置类型从相对到静态效果的转变?

javascript - css 选择器,为除两个标签之外的所有标签设置属性

javascript - 用动画增加数量

javascript - AngularJs 使用参数调用父 Controller 函数

java - MVC——循环依赖

javascript - 如何跟踪我的数据结构?