Example: calculate the date after the current time has passed 5 working days (set as N working days).
Train of thought:
Get the weekend and legal holiday days between the current system time + n (after N working days) +
Because legal holidays are immutable, we first set them as an array
//Holiday array
var Holiday = ["2018/1/1","2018/1/15","2018/2/19","2018/5/28","2018/7/4","2018/9/3","2018/10/8","2018/11/12","2018/11/22","2018/12/25"];
/**
* Loop to determine whether an element exists in an array
* @param {Object} arr array
* @param {Object} value Element value
*/
function isInArray(arr,value){
for(var i = 0; i < arr.length; i++){
if(value === arr[i]){
return true;
}
}
return false;
}
Then get the current system time
//Format the current system time format as (yyyy MM DD)
function times(){
var date = new Date();
var nowMonth = date.getMonth() + 1;
var hours = date.getHours();
if(hours >= 15){
var strDate = date.getDate();
}else{
var strDate = date.getDate()-1;
}
// Get the current number
//var strDate = date.getDate();
// Add separator '-'
var seperator = "-";
// Process the month, add a "0" before January to September
if (nowMonth >= 1 && nowMonth <= 9) {
nowMonth = "0" + nowMonth;
}
// Process the month, adding a "0" before the 1st-9th
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
// Finally, concatenate the string to get a date in the format of (yyyy MM DD)
var nowDate = date.getFullYear() + seperator + nowMonth + seperator + strDate;
return nowDate;
}
Here is the function to calculate holidays
/**
* Calculate the date after a working day of the current period
* @param {date} startDate current time
* @param {string} limitDay Working day
*/
function getWorkDate(startDate,limitDay){
var time = Date.parse(startDate);
var startTime = new Date(Date.parse(startDate));
var startTime = startTime.getTime();
var T = 24*60*60*1000;
var endTime = startTime+(limitDay*T);
if(limitDay>0){
var holidays = 0;
for(var i=startTime+T;i<=endTime;i+=T){
var date = new Date(i);
//Holiday logic here
if(date.getDay()==0 || date.getDay()==6){
holidays++;
}
//Determine whether the date is in the holiday array
if(isInArray(Holiday,date.toLocaleDateString()) == true){
holidays++;
}
}
return getWorkDate(new Date(endTime),holidays);
}else{
return startDate.toLocaleDateString();
}
}
instantiation
//Get current system time
var current_time = times();
//The format of the date after three working days is (YYYY-MM-dd)
var min_days = getWorkDate(current_time,"3");