为什么我的日历在12月下旬开始?

时间:2017-10-23 09:10:54

标签: javascript

我应该用html,css和javascript构建一个日历 - 没有php,没有jquery,没有。我整个周末一直在处理代码中的错误,但我不知道它来自哪里。我不知道该怎么办。这是我的代码:

"use strict";

window.onload = init;

function init() {
    var day = new Date();
    var month = day.getMonth() + 1;
    var year = day.getYear() + 1900;
    var next = document.getElementById("next");
    var previous = document.getElementById("previous");

    next.onclick = nextMonth(month);
    previous.onclick = previousMonth(month);

    generateCalendar(month, year);
}

function generateCalendar(_month, _year) {
    var table = document.getElementById("calendar");

    var month = ["Januar", "Februar", "März", "April", "Mai", "Juni",
    "Juli", "August", "September", "Oktober", "November", "Dezember"];
    var day = ["Mo", "Di", "Mi", "Do", "Fr", "Sa", "So"];
    var totaldays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

    //determining the first day of the month
    var firstday = new Date(_year, _month + 1, 1);
    var start = firstday.getDay() + 1;


    var end = totaldays[_month - 1]; //determining the length of the month

    //February
    if (end === totaldays[1]) {
        //leap years (% means modulo)
        if ((_year % 4 === 0 && _year % 100 !== 0) || _year % 400 === 0) {
            end = 29;
        }
    }

    //tale header
    var head = month[_month - 1] + " " + _year;
    var caption = table.createCaption();
    caption.innerHTML = head;

    var cell;
    var row = table.insertRow(0);
    for (var i = 0; i <= 6; i++) {
        cell = row.insertCell(i);
        cell.innerHTML = day[i];
    }

    var days = 1;

    for (var i = 0; i <= 5; i++) { //started week in a month
        row = table.insertRow(i + 1);

        for (var j = 0; j <= 6; j++) { //days of a week

            //regular calendar days
            if (days <= end && (i > 0 || j >= start)) {
                cell = row.insertCell(j);
                cell.innerHTML = days;
                days++;
            } else {

                //Days that aren't in the current month are shown as empty
                cell = row.insertCell(j);
                cell.innerHTML = "";
            }
        }
    }
}


function nextMonth(_month) {
    //contruction site
}

function previousMonth(_month) {
    //construction site
}

我的html文件几乎只有一个<table>,上面有id

所以日历显然是德语。正如你所看到的那样(我希望)我试着将这个月的第一天放在array的一个字段中,从0-6到我的工作日,这是非常基本的并且有效!

直到12月。 12月的第一天应该是星期五,但是在我的日历中它是在星期六,我不知道为什么它会提前一天跳。我调试了它,我看到的是它在我的阵列中跳过了一天。我想这与+1 firstday.getDay()有关,但我需要这样做,因为所有Date函数都是如此不一致。

0 个答案:

没有答案