gpt4 book ai didi

Javascript 日历在闰年期间跳过几天?

转载 作者:行者123 更新时间:2023-12-02 19:05:15 24 4
gpt4 key购买 nike

我用 javascript 和 jquery 编写了一个日历(改编自 "Simple Calendar with Javascript" ),它允许我一次前后移动几个月。

它通过每 4 年的 2 月增加一天来计算闰年,但似乎因此导致了错误。

当您向后或向前移动时,它会更改日期并根据新日期运行 calendar() 函数。通常,它有 5 行 7 列,每列包含一天。如果 5 行没有足够的空间,例如有 31 天,其中第一天是星期六,那么它将额外添加第 6 行来容纳。

但是,在闰年期间,如果发生上述相同情况,该函数似乎不会添加额外的行。

这是一个 jsFiddle:http://jsfiddle.net/charlescarver/DrMWM/

calendar() 的要点:

function calendar(d){

$("#calendar").remove();
$("body").append("<table border='1' id='calendar'></table>");

// Date vars
var today = new Date(d);
var month = today.getMonth();
var day = today.getDay();
var dayN = today.getDate();
var week = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var monthdays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var days = monthdays[month];
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

// Determine if leap year, if not, set to current year
if (month > 0) {
year = today.getYear();
if (year % 4 == 0){
days = 29;
}
} else {
year = today.getYear();
}

// Parse vars
var jumped = 0;
var inserted = 1;
var start = day - dayN%7 + 1;
if (start < 0) start += 7;
var weeks = parseInt((start + days)/7);
if ((start + days)%7 != 0){
weeks++;
}

$("#calendar").append("<tr><td class='month' colspan='7'>" + monthNames[month] + " - " + today.getFullYear() + "</td></tr>");

$("#calendar").append("<tr class='day'></tr>");
for (var i=0; i<7; i++) {
$(".day").append("<td>" + week[i][0] + "</td>");
}

function datDate(m, n, y){
date = new Date(" " + monthNames[m] + " " + n + " " + y);
return date;
}

for (var i=weeks; i>0; i--) {
if (i === i){
$("#calendar").append("<tr class=" + i + "></tr>");
for (var j=0; j<7; j++) {
console.log(inserted);
if (jumped<start || inserted>days) {
$("." + i).append("<td> </td>");
jumped++;
}
else {
m = month;
n = inserted;
y = today.getFullYear();
date = datDate(m, n, y);
if (inserted == dayN) {
$("." + i).append("<td><a class='calendar_date current_date' data-date='" + date + "'>[" + n + "]</a></td>");
} else {
$("." + i).append("<td><a class='calendar_date' data-date='" + date + "'>" + n + "</a></td>");
}
inserted++;
}
}
}
}
}

最佳答案

            // Determine if leap year, if not, set to current year
if (month == 1) {
year = today.getYear();
if (year % 4 == 0){
days = 29;
}
} else {
year = today.getYear();
}

关于Javascript 日历在闰年期间跳过几天?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14369101/

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