JS实现的一个比较不错的判断节假日的实现代码(假日包括周末,不包括调休上班的周末)
作者:勤思而敏学
最近需要实现一个判断今天是不是节假日的功能,考虑周末与法定节假日调休等,这样的比较完整的功能就需要下面的代码来实现了
思路:创建两个数组,数组1为节假日数组,数组2为是周末上班日期数组。如果当前日期(或某日期)同时满足2个条件(1.在节假日数组内或在周末。2.不在周末上班日期数组)即为节假日,否则即为非假日。
注意:两个数组日期为2024-4-4(而不是2024-04-04),因为curdate.getMonth()和curdate.getDate()返回的是4,而不是04。
//节假日数组
var holidays = [
'2024-4-4', //清明
'2024-4-5', //清明
'2024-4-6', //清明
'2024-5-1', // 劳动节
'2024-5-2', // 劳动节
'2024-5-3', // 劳动节
'2024-5-4', // 劳动节
'2024-5-5', // 劳动节
'2024-6-8', // 端午节
'2024-6-9', // 端午节
'2024-6-10', // 端午节
'2024-9-15', // 中秋节
'2024-9-16', // 中秋节
'2024-9-17', // 中秋节
'2024-10-1', // 国庆节
'2024-10-2', // 国庆节
'2024-10-3', // 国庆节
'2024-10-4', // 国庆节
'2024-10-5', // 国庆节
'2024-10-6', // 国庆节
'2024-10-7', // 国庆节
'2023-12-31' // 元旦
];
//周末上班日期数组
var nWeekend = [
'2024-4-7', //清明调整
'2024-4-28', //五一调整
'2024-5-11', //五一调整
'2024-9-14', //中秋调整
'2024-9-29', //国庆调整
'2024-10-12', //国庆调整
];
var curdate = new Date();
curdate.setTime(curdate.getTime() + 4 * 24 * 60 * 60 * 1000); // 1即明天,2即后天
var year = curdate.getFullYear();
var month = curdate.getMonth()+1 ;//getMonth()+1为当前月份
var date = curdate.getDate();
var formattedDate = year + "-" + month + "-" + date;
//该日期同时满足2个条件即为节假日:1.在节假日数组内或在周末. 2.不在周末上班日期数组
if((holidays.indexOf(formattedDate)>=0 || (curdate.getDay()==0 || curdate.getDay()==6)) && nWeekend.indexOf(formattedDate)<0){
console.log(formattedDate+':'+'节假日');
}else{
console.log(formattedDate+':'+'非节假日');
}如下,当前日期2024-4-3:非节假日。

如下,当前日期加1为2024-4-4:节假日(清明节)。

如下,当前日期加4为2024-4-7:非节假日(虽是周日,但属于清明节调整的上班日)。

js 实现判断节假日
var date = new Date();
// 获取月份(注意月份从0开始计数)
var month = date.getMonth() + 1; // 需要加上1才能得到正确的月份值
// 根据不同的月份设置相应的节日信息
if (month === 1 && date.getDate() === 1) {
console.log("今天是元旦");
} else if (month === 2 && date.getDate() === 14) {
console.log("今天是情人节");
} else if (month === 3 && date.getDate() === 8) {
console.log("今天是妇女节");
} else if (month === 4 && date.getDate() === 4) {
console.log("今天是清明节");
} else if (month === 5 && date.getDate() === 1) {
console.log("今天是劳动节");
} else if (month === 5 && date.getDay() === 0) {
console.log("今天是母亲节");
} else if (month === 6 && date.getDay() === 0) {
console.log("今天是父亲节");
} else if (month === 6 && date.getDate() >= 24 && date.getDate() <= 27) {
console.log("今天是端午节");
} else if (month === 9 && date.getDate() === 10) {
console.log("今天是教师节");
} else if (month === 10 && date.getDate() === 1) {
console.log("今天是国庆节");
} else if (month === 12 && date.getDate() === 25) {
console.log("今天是圣诞节");
} else {
console.log("今天没有特定的节日");
}
js 实现判断节假日2
在JavaScript中,判断节假日通常需要一个已知的节假日列表,然后将日期与这个列表进行比较。以下是一个简单的实现示例:
// 假设的节假日列表
const holidays = [
{ date: '2024-01-01', name: '元旦' },
{ date: '2024-02-15', name: '春节' },
{ date: '2024-04-05', name: '清明节' },
// ... 其他节假日
];
// 判断给定日期是否为节假日的函数
function isHoliday(date) {
const dateString = date.toISOString().split('T')[0];
return holidays.some(holiday => holiday.date === dateString);
}
// 使用示例
const testDate = new Date('2023-02-15');
console.log(isHoliday(testDate)); // 输出: true
const anotherDate = new Date('2023-03-01');
console.log(isHoliday(anotherDate)); // 输出: false在这个示例中,holidays 数组包含了2023年的部分节假日。isHoliday 函数接收一个Date对象作为参数,将其转换为字符串格式的日期,然后与预定义的节假日列表中的日期进行比较。如果找到匹配项,则返回true,表示给定的日期是节假日;否则返回false。
javascript中Date对象周六日判断
// 创建一个Date对象,使用当前时间
const currentDate = new Date();
// 获取星期几的值
const dayOfWeek = currentDate.getDay();
// 判断当前日期是否为周六或周日,并输出结果
if (dayOfWeek === 6) {
console.log("今天是周六。");
} else if (dayOfWeek === 0) {
console.log("今天是周日。");
} else {
console.log("今天不是周末。"或者
function isWeekend() {
const date = new Date();
const day = date.getDay();
return day === 0 || day === 6; // 0代表周日,6代表周六
}
实际应用
function iswktime() {
const date = new Date();
const day = date.getDay();
var isweekday=false;
if (day===0 || day === 6){
isweekday=true;
}
if (isweekday===true) {
return false;
}else{
if ((date.getHours() >= 8 && date.getHours() <= 18)) {
return true;
} else {
return false;
}
}
}到此这篇关于JS实现的一个比较不错的判断节假日的实现代码(假日包括周末,不包括调休上班的周末)的文章就介绍到这了,更多相关JS判断节假日内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
