gpt4 book ai didi

JavaScript 获取有单词的天数,比当前日期提前 5 天

转载 作者:行者123 更新时间:2023-11-28 20:29:14 26 4
gpt4 key购买 nike

我想在我网站上的一些 div 中放置 5 个即将到来的日子的名称。我已经为日期名称创建了数组,然后从中调用值并添加天数。该方法仅适用于提前第一天,然后数组结束,从第 2 天到第 5 天,它回显“未定义”。

请帮忙吗?

var weekday=new Array(7);
weekday[0]="Nedelja";
weekday[1]="Ponedeljek";
weekday[2]="Torek";
weekday[3]="Sreda";
weekday[4]="Četrtek";
weekday[5]="Petek";
weekday[6]="Sobota";
var dan = weekday[today.getDay()];

var dan_1 = weekday[today.getDay()+1];
document.getElementById("day1").innerHTML = dan_1;
var dan_2 = weekday[today.getDay()+2];
document.getElementById("day2").innerHTML = dan_2;
var dan_3 = weekday[today.getDay()+3];
document.getElementById("day3").innerHTML = dan_3;
var dan_4 = weekday[today.getDay()+4];
document.getElementById("day4").innerHTML = dan_4;
var dan_5 = weekday[today.getDay()+5];
document.getElementById("day5").innerHTML = dan_5;

谢谢大家!

最佳答案

那么,你需要用 7(一周的天数)来计算 %

var weekday=new Array(7);
weekday[0]="Nedelja";
weekday[1]="Ponedeljek";
weekday[2]="Torek";
weekday[3]="Sreda";
weekday[4]="Četrtek";
weekday[5]="Petek";
weekday[6]="Sobota";
var dan = weekday[today.getDay()%7];

var dan_1 = weekday[(today.getDay()+1)%7];
document.getElementById("day1").innerHTML = dan_1;
var dan_2 = weekday[(today.getDay()+2)%7];
document.getElementById("day2").innerHTML = dan_2;
var dan_3 = weekday[(today.getDay()+3)%7];
document.getElementById("day3").innerHTML = dan_3;
var dan_4 = weekday[(today.getDay()+4)%7];
document.getElementById("day4").innerHTML = dan_4;
var dan_5 = weekday[(today.getDay()+5)%7];
document.getElementById("day5").innerHTML = dan_5;

例如:如果 today.getDay() 返回 5,则接下来的 5 天是 10,该值在数组中不存在。当你 % 7 时,你会得到 3(下周)

关于JavaScript 获取有单词的天数,比当前日期提前 5 天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16735867/

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