gpt4 book ai didi

javascript - 检查今天是否在日期数组中

转载 作者:行者123 更新时间:2023-11-28 16:54:53 25 4
gpt4 key购买 nike

我正在尝试运行一个脚本,根据预定的日期数组(公共(public)假期)检查今天的日期。但是,我很难理解阵列设置。我已经让脚本在没有使用单个日期的数组的情况下工作,但是不知道如何将其转换为具有多个选项进行检查的数组。

编写的代码如下;

var today = new Date().getHours();
var daycheck = new Date().getDay();
var holidayArray = new Date('12/19/2019');
var todayDate = new Date();

if (holidayArray.setHours(0, 0, 0, 0) == todayDate.setHours(0, 0, 0, 0)) {
alert('This is outside of business hours (Public Holiday).');
window.history.back();
} else {
if (today <= 8 || today >= 17) {
alert('This is outside of business hours. General access is provided from 9am - 5pm Monday to Friday');
window.history.back();
}
else {
if (daycheck == 0 || daycheck == 6) {
alert('This is outside of business hours(weekend).');
window.history.back();
}
else {
if (confirm('It is currently within business hours during the week. You can now continue.')) {
window.location.href = "https://URL";
}
else { window.history.back() }
}
}
}

我需要帮助向 holidayArray 变量添加其他日期,因为每次我尝试某些操作时它都不起作用。

提前致谢

最佳答案

在 JavaScript 中,您可以通过以下语法创建数组: var HolidayArray = [new Date("12/19/2019"), new Date("12/20/2019"), new Date("12/21")/2019")]

然后你必须循环遍历这个数组,例如:

for (let holiday of holidayArray) {
// Place your IF statements in here and make your checks against the value "holiday". For example:

if (holiday.setHours(0,0,0,0) == todayDate.setHours(0,0,0,0)) {
alert("This is outside of business hours (Public Holiday).");
window.history.back();
}

}

这将按顺序循环遍历您的数组。 holiday 将代表数组中的每个值。

关于javascript - 检查今天是否在日期数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59400699/

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