How to determine holidays in js
Because I want to make a calendar control and encounter national statutory holidays, how to implement this function? In fact, the implementation code is very simple. Below I will share with you the js holiday judgment example code. Friends who need it can refer to it
Effect display:
<!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年节假日 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年调休日 var txr_2017 = ["2017-01-22","2017-02-04","2017-04-01","2017-05-27","2017-09-30"]; //2018年节假日 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年调休日 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; //"节假日"; } var getday = date.getDay(); if (getday == 0 || getday == 6) { if(is_tx_2017!=-1 || is_tx_2018!=-1){ num++; return;// '调休日'; } return;// '休息日'; } else { if(is_tx_2017!=-1 || is_tx_2018!=-1){ num++; return;// '调休日'; } num++; return;// '工作日'; } //num; } //var d_arr = []; //时间段内所有日期 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("呀!这段时间我们一共上"+num+"天班呢。"); } function getInfo(info){ var startDate = $("#startDate").val(); var endDate = $("#endDate").val(); getDayInfo(startDate,endDate); //console.log(isWorkday(startDate)); } // 调用 //alert(isWorkday('2016-12-10')); </script> <body> <h3>填写时间算算我们能上几天班?!</h3> <p id="p1"> 开始时间:<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="点击获取工作日天数"></input> </p> <p> <strong><span id = "show"></span></strong> </p> </body> </html>
ps: Let me share with you a piece of code to determine all holidays based on js. The specific code is as follows:
// 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("春节/情人节"); if ((month == 2) && (date == 1)) document.write("国际海豹日"); if ((month == 2) && (date == 8)) document.write("国际劳动妇女节/中国保护母亲河日"); if ((month == 2) && (date == 12)) document.write("植树节"); if ((month == 3) && (date == 1)) document.write("愚人节"); if ((month == 3) && (date == 5)) document.write("清明节"); if ((month == 4) && (date == 1)) document.write("国际劳动节"); if ((month == 4) && (date == 9)) document.write("母亲节"); if ((month == 5) && (date == 1)) document.write("国际儿童节"); if ((month == 5) && (date == 26)) document.write("国际禁毒日"); if ((month == 7) && (date == 1)) document.write("建军节"); if ((month == 7) && (date == 15)) document.write("日本无条件投降日/世纪婚纱日"); if ((month == 7) && (date == 16)) document.write("七夕情人节"); if ((month == 9) && (date == 20)) document.write("世界厨师日"); if ((month == 9) && (date == 22)) document.write("世界传统医药日"); if ((month == 9) && (date == 24)) document.write("联合国日/世界发展信息日"); if ((month == 9) && (date == 25)) document.write("世界骨质疏松日/抗美援朝纪念日/环卫工人节"); if ((month == 9) && (date == 31)) document.write("世界勤俭日/中国男性健康日"); if ((month == 11) && (date == 24)) document.write("平安夜"); if ((month == 11) && (date == 25)) document.write("圣诞节");
The above is what I compiled for you. I hope it will be helpful to you in the future.
Related articles:
How to implement the message board function in JS
How to implement OAuth2.0 authorization service authentication in nodejs
How to build a vue project on webpack
The above is the detailed content of How to determine holidays in js. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

How to use JS and Baidu Map to implement map pan function Baidu Map is a widely used map service platform, which is often used in web development to display geographical information, positioning and other functions. This article will introduce how to use JS and Baidu Map API to implement the map pan function, and provide specific code examples. 1. Preparation Before using Baidu Map API, you first need to apply for a developer account on Baidu Map Open Platform (http://lbsyun.baidu.com/) and create an application. Creation completed

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

Overview of how to use JS and Baidu Maps to implement map click event processing: In web development, it is often necessary to use map functions to display geographical location and geographical information. Click event processing on the map is a commonly used and important part of the map function. This article will introduce how to use JS and Baidu Map API to implement the click event processing function of the map, and give specific code examples. Steps: Import the API file of Baidu Map. First, import the file of Baidu Map API in the HTML file. This can be achieved through the following code:

How to use JS and Baidu Maps to implement the map heat map function Introduction: With the rapid development of the Internet and mobile devices, maps have become a common application scenario. As a visual display method, heat maps can help us understand the distribution of data more intuitively. This article will introduce how to use JS and Baidu Map API to implement the map heat map function, and provide specific code examples. Preparation work: Before starting, you need to prepare the following items: a Baidu developer account, create an application, and obtain the corresponding AP

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.
