如果在我的函数Kalender()
内部,我有一个闰年但由于某种原因它在闰年的2月份崩溃(例如2012年)。通过调试我没有错误。由于某种原因,它不会将monthLength [1]更改为28?
var dayNames = ['Zon', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
//Volledige Dagnamen//
var dayNamesFull = ['Zondag', 'Maandag', 'Dinsdag', 'Woensdag', 'Donderdag', 'Vrijdag', 'Zaterdag'];
//Volledige Maandnamen//
var monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'Oktober', 'November', 'December'];
//Maand lengte van 0 - 11//
var monthLength = [31,28,31,30,31,30,31,31,30,31,30,31];
//Nieuwe datum//
var today = new Date();
//Vandaag//
var day = today.getDay();
var currentDay = day;
//vandaag zoekmaand//
var month = today.getMonth();
var currentMonth= month;
//vandaag//
var vandaag = today.getDate();
//volledig jaar//
var Leapyear = today.getYear();
var year = today.getFullYear();
var currentYear = year;
//events in een array//
var act = ['Opa en Oma 50 jaar getrouwd','Afspraak met de baas', 'Lunchen met Oma','Kleding kopen met mijn moeder','Frank gaat verhuizen', 'School opdracht inleveren','Feestje van Henk', 'Kijken voor een nieuwe auto','Kinderen van school halen','Afspraak doktor 15.00','Zalf halen voor mijn voeten','Onderhoudsbeurt auto','Hond uitlaten','Vaccin krijgen voor reis naar jakarta','Vliegticket boeken', 'Kamer opruimen', 'Hond wegbrengen voor vlooienbehandeling', 'Ov-chipkaart opladen met geld', 'APK keuring Auto', 'Nieuwe velgen voor de auto ophalen in Loenen ad Vecht','Paardenstal opruimen','Golfen met mijn vrienden','Uit eten met mijn schoonfamilie','Werkoverleg overname', ' Bedrijfsbeleid aanpassen'];
//Kalender schrijf functie//
function Kalender() {
var buttons = '<button id="vorige" onclick="vorige()">Vorige</button><button id="volgende" onclick="volgende()">Volgende</button>'
var kalender = "";
//Leap Year//
if (month == 1){
if (year % 4 == 0){
monthLength = 29;
}
}
document.getElementById('Header').innerHTML = monthNames[month]+" "+year+buttons;
kalender += '<table id="cal"><div id="taken"></div><div id="events"></div>';
for (var j = 1; j <= monthLength[month]; j++) {
if ((vandaag == j) && (currentMonth == month) && (currentYear == year)) {
kalender += "<td class='tabeldagen' id='dagVandaag'><a id='dag"+j+"' onclick='addEvent (this.id)'>" + j;
}
else {
kalender += "<td id='"+j+"' class='tabeldagen'><a id='dag"+j+"' onclick='addEvent (this.id)'>"+j;
}
if (j % 7 == 0) {
kalender += "<tr>";
}
kalender += '</td></a>';
}
kalender += '</table>';
document.getElementById('kalen').innerHTML = kalender;
document.getElementById('taken').innerHTML = dayNamesFull[currentDay]+" "+vandaag+" "+monthNames[currentMonth];
}
//Volgende Maand functie//
function volgende() {
month = month + 1;
if(month > 11) {
month = -1;
month = month + 1;
year = year + 1;
}
Kalender();
}
//Vorige maand functie//
function vorige() {
month = month - 1;
if(month < 0) {
month = + 12 ;
month = month - 1;
year = year - 1;
}
Kalender();
}
//Voegt event toe aan De desbetreffende dag//
function addEvent (id) {
if ((year == 2014) && (month == 10)) {
switch (id) {
case "dag1":
document.getElementById("events").innerHTML = act[1];
break;
case "dag2":
document.getElementById("events").innerHTML = act[2];
break;
case "dag3":
document.getElementById("events").innerHTML = act[0];
break;
case "dag4":
document.getElementById("events").innerHTML = act[3];
break;
case "dag5":
document.getElementById("events").innerHTML = act[4];
break;
case "dag6":
document.getElementById("events").innerHTML = act[5];
break;
case "dag7":
document.getElementById("events").innerHTML = act[6];
break;
case "dag8":
document.getElementById("events").innerHTML = act[7];
break;
case "dag9":
document.getElementById("events").innerHTML = act[8];
break;
case "dag10":
document.getElementById("events").innerHTML = act[9];
break;
case "dag11":
document.getElementById("events").innerHTML = act[10];
break;
case "dag12":
document.getElementById("events").innerHTML = act[11];
break;
case "dag13":
document.getElementById("events").innerHTML = act[12];
break;
case "dag14":
document.getElementById("events").innerHTML = act[13];
break;
case "dag15":
document.getElementById("events").innerHTML = act[14];
break;
case "dag16":
document.getElementById("events").innerHTML = act[15];
break;
case "dag17":
document.getElementById("events").innerHTML = act[16];
break;
case "dag18":
document.getElementById("events").innerHTML = act[17];
break;
case "dag19":
document.getElementById("events").innerHTML = act[18];
break;
case "dag20":
document.getElementById("events").innerHTML = act[19];
break;
case "dag21":
document.getElementById("events").innerHTML = act[20];
break;
case "dag22":
document.getElementById("events").innerHTML = act[21];
break;
case "dag23":
document.getElementById("events").innerHTML = act[22];
break;
case "dag24":
document.getElementById("events").innerHTML = act[23];
break;
case "dag25":
document.getElementById("events").innerHTML = act[24];
break;
case "dag26":
document.getElementById("events").innerHTML = act[2];
break;
case "dag27":
document.getElementById("events").innerHTML = act[6];
break;
case "dag28":
document.getElementById("events").innerHTML = act[9];
break;
case "dag29":
document.getElementById("events").innerHTML = act[12];
break;
case "dag30":
document.getElementById("events").innerHTML = act[11];
break;
case "dag31":
document.getElementById("events").innerHTML = act[0];
break;
}
}
}
答案 0 :(得分:2)
您的leapYear
测试不正确,此
if (year % 4 == 0) {
应该是
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
维基百科将Leap year algorithm视为
if (year is not divisible by 4) then (it is a common year) else if (year is not divisible by 100) then (it is a leap year) else if (year is not divisible by 400) then (it is a common year) else (it is a leap year)
修改强>
抱歉,在Kalendar
monthLength
中还有一个数组 -
//Leap Year//
if (month == 1){
monthLength[1] = 28;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
monthLength[1] = 29;
}
}