gpt4 book ai didi

javascript - 闰年功能不起作用

转载 作者:行者123 更新时间:2023-11-30 17:12:29 26 4
gpt4 key购买 nike

如果在我的函数 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;
}
}
}

http://jsfiddle.net/nshd0o47/1/

最佳答案

你的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)

编辑

抱歉,在 KalendarmonthLength 也是一个数组 -

//Leap Year//
if (month == 1){
monthLength[1] = 28;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
monthLength[1] = 29;
}
}

关于javascript - 闰年功能不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26716406/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com