js判斷節(jié)假日實(shí)例代碼
效果展示:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="jquery.min.js" type="text/javascript"></script>
<script src="date.js" type="text/javascript"></script>
</head>
<script>
/*************************************************************
* 判斷某天是不是工作日
*
* @ date {Date} 要判斷的日期(0000-00-00)
**************************************************************/
//2017年節(jié)假日
var jrdate_2017 = ["2017-01-01","2017-01-02","2017-01-27","2017-01-27","2017-01-27","2017-01-28","2017-01-29","2017-01-30","2017-01-31","2017-02-01","2017-02-02","2017-04-02","2017-04-03","2017-04-04","2017-04-29","2017-04-30","2017-05-01","2017-05-28","2017-05-29","2017-05-30","2017-10-01","2017-10-02","2017-10-03","2017-10-04","2017-10-05","2017-10-06","2017-10-07","2017-10-08"];
//2017年調(diào)休日
var txr_2017 = ["2017-01-22","2017-02-04","2017-04-01","2017-05-27","2017-09-30"];
//2018年節(jié)假日
var jrdate_2018 = ["2018-01-01","2018-02-15","2018-02-16","2018-02-17","2018-02-18","2018-02-19","2018-02-20","2018-02-21","2018-04-05","2018-04-06","2018-04-07","2018-04-29","2018-04-30","2018-05-01","2018-06-16","2018-06-17","2018-06-18","2018-09-22","2018-09-23","2018-09-24","2018-10-01","2018-10-02","2018-10-03","2018-10-04","2018-10-05","2018-10-06","2018-10-07"];
//2018年調(diào)休日
var txr_2018 = ["2018-02-11","2018-02-24","2018-04-08","2018-04-28","2018-09-29","2018-09-30"];
var num = 0;
function isWorkday(date_String) {
//debugger;
var date = !date_String ? new Date() : new Date(date_String);
var is_jr_2017 = $.inArray(date_String, jrdate_2017);
var is_jr_2018 = $.inArray(date_String, jrdate_2018);
var is_tx_2017 = $.inArray(date_String, txr_2017);
var is_tx_2018 = $.inArray(date_String, txr_2018);
if(is_jr_2017!=-1 || is_jr_2018!=-1){
return; //"節(jié)假日";
}
var getday = date.getDay();
if (getday == 0 || getday == 6) {
if(is_tx_2017!=-1 || is_tx_2018!=-1){
num++;
return;// '調(diào)休日';
}
return;// '休息日';
}
else {
if(is_tx_2017!=-1 || is_tx_2018!=-1){
num++;
return;// '調(diào)休日';
}
num++;
return;// '工作日';
}
//num;
}
//var d_arr = []; //時(shí)間段內(nèi)所有日期
function getDayInfo(startDate,endDate){
var bd = new Date(startDate),be = new Date(endDate);
var bd_time = bd.getTime(), be_time = be.getTime(),time_diff = be_time - bd_time;
d_arr = [];
num = 0;
for(var i=0; i<= time_diff; i+=86400000){
var ds = new Date(bd_time+i).Format("yyyy-MM-dd");
isWorkday(ds);
//d_arr.push(ds)
}
console.log(num);
console.log(d_arr);
$("#show").html("呀!這段時(shí)間我們一共上"+num+"天班呢。");
}
function getInfo(info){
var startDate = $("#startDate").val();
var endDate = $("#endDate").val();
getDayInfo(startDate,endDate);
//console.log(isWorkday(startDate));
}
// 調(diào)用
//alert(isWorkday('2016-12-10'));
</script>
<body>
<h3>填寫時(shí)間算算我們能上幾天班?!</h3>
<div id="div1">
開始時(shí)間:<input id="startDate" type="text" value="2018-02-01"></input>
截止日期:<input id="endDate" type="text" value="2018-02-28"></input>
<input type="button" onclick = 'getInfo()' value="點(diǎn)擊獲取工作日天數(shù)"></input>
</div>
<div>
<strong><span id = "show"></span></strong>
</div>
</body>
</html>ps:下面在給大家分享一段代碼基于js判斷所有節(jié)假日,具體代碼如下所示:
// JavaScript Document
calendar = new Date();
month = calendar.getMonth();
date = calendar.getDate();
if ((month == 0) && (date == 1)) document.write("元旦");
if ((month == 1) && (date ==13 )) document.write("除夕");
if ((month == 1) && (date ==14 )) document.write("春節(jié)/情人節(jié)");
if ((month == 2) && (date == 1)) document.write("國(guó)際海豹日");
if ((month == 2) && (date == 8)) document.write("國(guó)際勞動(dòng)?jì)D女節(jié)/中國(guó)保護(hù)母親河日");
if ((month == 2) && (date == 12)) document.write("植樹節(jié)");
if ((month == 3) && (date == 1)) document.write("愚人節(jié)");
if ((month == 3) && (date == 5)) document.write("清明節(jié)");
if ((month == 4) && (date == 1)) document.write("國(guó)際勞動(dòng)節(jié)");
if ((month == 4) && (date == 9)) document.write("母親節(jié)");
if ((month == 5) && (date == 1)) document.write("國(guó)際兒童節(jié)");
if ((month == 5) && (date == 26)) document.write("國(guó)際禁毒日");
if ((month == 7) && (date == 1)) document.write("建軍節(jié)");
if ((month == 7) && (date == 15)) document.write("日本無條件投降日/世紀(jì)婚紗日");
if ((month == 7) && (date == 16)) document.write("七夕情人節(jié)");
if ((month == 9) && (date == 20)) document.write("世界廚師日");
if ((month == 9) && (date == 22)) document.write("世界傳統(tǒng)醫(yī)藥日");
if ((month == 9) && (date == 24)) document.write("聯(lián)合國(guó)日/世界發(fā)展信息日");
if ((month == 9) && (date == 25)) document.write("世界骨質(zhì)疏松日/抗美援朝紀(jì)念日/環(huán)衛(wèi)工人節(jié)");
if ((month == 9) && (date == 31)) document.write("世界勤儉日/中國(guó)男性健康日");
if ((month == 11) && (date == 24)) document.write("平安夜");
if ((month == 11) && (date == 25)) document.write("圣誕節(jié)");JS——判斷節(jié)假日(假日包括周末,不包括調(diào)休上班的周末)
//節(jié)假日數(shù)組
var holidays = [
'2024-4-4', //清明
'2024-4-5', //清明
'2024-4-6', //清明
'2024-5-1', // 勞動(dòng)節(jié)
'2024-5-2', // 勞動(dòng)節(jié)
'2024-5-3', // 勞動(dòng)節(jié)
'2024-5-4', // 勞動(dòng)節(jié)
'2024-5-5', // 勞動(dòng)節(jié)
'2024-6-8', // 端午節(jié)
'2024-6-9', // 端午節(jié)
'2024-6-10', // 端午節(jié)
'2024-9-15', // 中秋節(jié)
'2024-9-16', // 中秋節(jié)
'2024-9-17', // 中秋節(jié)
'2024-10-1', // 國(guó)慶節(jié)
'2024-10-2', // 國(guó)慶節(jié)
'2024-10-3', // 國(guó)慶節(jié)
'2024-10-4', // 國(guó)慶節(jié)
'2024-10-5', // 國(guó)慶節(jié)
'2024-10-6', // 國(guó)慶節(jié)
'2024-10-7', // 國(guó)慶節(jié)
'2023-12-31' // 元旦
];
//周末上班日期數(shù)組
var nWeekend = [
'2024-4-7', //清明調(diào)整
'2024-4-28', //五一調(diào)整
'2024-5-11', //五一調(diào)整
'2024-9-14', //中秋調(diào)整
'2024-9-29', //國(guó)慶調(diào)整
'2024-10-12', //國(guó)慶調(diào)整
];
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為當(dāng)前月份
var date = curdate.getDate();
var formattedDate = year + "-" + month + "-" + date;
//該日期同時(shí)滿足2個(gè)條件即為節(jié)假日:1.在節(jié)假日數(shù)組內(nèi)或在周末. 2.不在周末上班日期數(shù)組
if((holidays.indexOf(formattedDate)>=0 || (curdate.getDay()==0 || curdate.getDay()==6)) && nWeekend.indexOf(formattedDate)<0){
console.log(formattedDate+':'+'節(jié)假日');
}else{
console.log(formattedDate+':'+'非節(jié)假日');
}總結(jié)
以上所述是小編給大家介紹的js判斷節(jié)假日實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
javascript實(shí)現(xiàn)簡(jiǎn)單加載隨機(jī)色方塊
這篇文章主要介紹了javascript實(shí)現(xiàn)簡(jiǎn)單加載隨機(jī)色方塊的相關(guān)資料,感興趣的小伙伴們可以參考一下2015-12-12
微信小程序?qū)崿F(xiàn)倒計(jì)時(shí)補(bǔ)零功能
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)倒計(jì)時(shí)補(bǔ)零功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
UniApp使用manifest.json應(yīng)用配置的超詳細(xì)教學(xué)
這篇文章主要給大家介紹了關(guān)于uni-app應(yīng)用配置manifest.json最全最詳細(xì)配置,manifest.json文件是應(yīng)用的配置文件,用于指定應(yīng)用的名稱、圖標(biāo)、權(quán)限等,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
iframe的onload在Chrome/Opera中執(zhí)行兩次Bug的解決方法
創(chuàng)建iframe對(duì)象,添加load事件, 再將iframe添加到body中。Chrome/Opera中會(huì)造成load事件的handler執(zhí)行兩次。2011-03-03
學(xué)習(xí)javascript的閉包,原型,和匿名函數(shù)之旅
Javascript中有幾個(gè)非常重要的語(yǔ)言特性——對(duì)象、原型繼承、閉包。其中閉包 對(duì)于那些使用傳統(tǒng)靜態(tài)語(yǔ)言C/C++的程序員來說是一個(gè)新的語(yǔ)言特性,本文給大家介紹js的閉包,原型,和匿名函數(shù)之旅,感興趣的朋友一起學(xué)習(xí)吧2015-10-10
解析img圖片沒找到onerror事件 Stack overflow at line: 0
本篇文章主要介紹了img圖片沒找到onerror事件 Stack overflow at line: 0 需要的朋友可以過來參考下,希望對(duì)大家有所幫助2013-12-12
javascript中使用正則計(jì)算中文長(zhǎng)度的例子
這篇文章主要介紹了javascript中使用正則計(jì)算中文長(zhǎng)度的例子,需要的朋友可以參考下2014-04-04

