JavaScript日历中的日期方法

时间:2014-12-19 04:05:25

标签: javascript function date

我正在制作日历,我想要更改月份,日期无法使其生效。 change()函数有效但如果我将选项从12月更改为1月,则函数会更改月份而不是日期。我的代码如下。

onchange函数提供了我需要的变量,因为我已使用alert()进行了检查。我认为我get_calender()函数中的onchange是我遇到的问题,但是无法找到它。如何更改我的代码以获得我想要的行为?

  var d = new Date();
  var year_name =['2014','2015','2016'];
  var month_name = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
  var month = d.getMonth(); 
  var year = d.getFullYear(); 
  var first_date = month_name[month] + " " + 1 + " " + year;
  var dagen = new Date(first_date);
  var start = dagen.getDay();
  var day_name = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
  var day_no = day_name[start];
  var days = new Date(year, month +1, 0).getDate();

window.onload = function(){
    var sel = maandgen();
    var ja = jaargen();
    var calendar = get_calendar();
    document.getElementById("calendar-month").appendChild(sel);
    document.getElementById("calendar-year").appendChild(ja);
    document.getElementById("calendar-dates").appendChild(calendar);    
}
function get_calendar(){


  var table = document.createElement('table');
  var tr = document.createElement('tr');
  //row for the day letters
  for(var c=0; c<=6; c++){
    var day_name = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
    var td = document.createElement('td');
    td.innerHTML = day_name[c];
    tr.appendChild(td);
  }
  table.appendChild(tr);
  //create 2nd row
  tr = document.createElement('tr');
  var c;
  for(c=0; c<=6; c++){
    if(c == day_no){
      break;
    }
    var td = document.createElement('td');
    td.innerHTML = "";
    tr.appendChild(td);
  }
  var count = 1;
  for(; c<=6; c++){
    var td = document.createElement('td');
    td.innerHTML =  "<button onclick= afspraak() id="+c+">"+ count;
    count++;
    tr.appendChild(td);
  }
  table.appendChild(tr);
  //rest of the date rows
  for(var r=3; r<=7; r++){
    var tr = document.createElement('tr');
    for(var c=0; c<=6; c++){
      if(count > days){
        table.appendChild(tr);
        return table;
      }
    for(var i=7;i<=31;i++){
        for(var d=7;d<=31;d++){

      var td = document.createElement('td');
      td.innerHTML =  "<button onclick= afspraak() id="+i+">"+ count;}}//}
      count++;
      tr.appendChild(td);
    }
    table.appendChild(tr);
  }
  return table;
}

function change(maand){

if(maand.value == "0"){
    var first = month_name[0] + " " + 1 + " " + year;
    var dagen = new Date(first);

    var start = dagen.getDay();
    var day_no = day_name[start];
    var days = new Date(year, month +1, 0).getDate();

    get_calendar();
    //alert();
}
}

0 个答案:

没有答案