js 两个时间之间工作日的计算问题(包含节假日)
程序员文章站
2022-05-16 21:26:16
...
录用2018年和2019年所有节假日期
<script>
let arr = ["2018/01/01", "2018/01/06", "2018/01/07", "2018/01/13", "2018/01/14", "2018/01/20", "2018/01/21", "2018/01/27", "2018/01/28", "2018/02/03", "2018/02/04", "2018/02/10", "2018/02/15", "2018/02/16", "2018/02/17", "2018/02/18", "2018/02/19", "2018/02/20", "2018/02/21", "2018/02/25", "2018/03/03", "2018/03/04", "2018/03/10", "2018/03/11", "2018/03/17", "2018/03/18", "2018/03/24", "2018/03/25", "2018/03/31", "2018/04/01", "2018/04/05", "2018/04/06", "2018/04/07", "2018/04/14", "2018/04/15", "2018/04/21", "2018/04/22", "2018/04/29", "2018/04/30", "2018/05/01", "2018/05/05", "2018/05/06", "2018/05/12", "2018/05/13", "2018/05/19", "2018/05/20", "2018/05/26", "2018/05/27", "2018/06/02", "2018/06/03", "2018/06/09", "2018/06/10", "2018/06/16", "2018/06/17", "2018/06/18", "2018/06/23", "2018/06/24", "2018/06/30", "2018/07/01", "2018/07/07", "2018/07/08", "2018/07/14", "2018/07/15", "2018/07/21", "2018/07/22", "2018/07/28", "2018/07/29", "2018/08/04", "2018/08/05", "2018/08/11", "2018/08/12", "2018/08/18", "2018/08/19", "2018/08/25", "2018/08/26", "2018/09/01", "2018/09/02", "2018/09/08", "2018/09/09", "2018/09/15", "2018/09/16", "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/10/13", "2018/10/14", "2018/10/20", "2018/10/21", "2018/10/27", "2018/10/28", "2018/11/03", "2018/11/04", "2018/11/10", "2018/11/11", "2018/11/17", "2018/11/18", "2018/11/24", "2018/11/25", "2018/12/01", "2018/12/02", "2018/12/08", "2018/12/09", "2018/12/15", "2018/12/16", "2018/12/22", "2018/12/23", "2018/12/30", "2018/12/31", "2019/01/01", "2019/01/05", "2019/01/06", "2019/01/12", "2019/01/13", "2019/01/19", "2019/01/20", "2019/01/26", "2019/01/27", "2019/02/04", "2019/02/05", "2019/02/06", "2019/02/07", "2019/02/08", "2019/02/09", "2019/02/10", "2019/02/16", "2019/02/17", "2019/02/23", "2019/02/24", "2019/03/02", "2019/03/03", "2019/03/09", "2019/03/10", "2019/03/16", "2019/03/17", "2019/03/23", "2019/03/24", "2019/03/30", "2019/03/31", "2019/04/05", "2019/04/06", "2019/04/07", "2019/04/13", "2019/04/14", "2019/04/20", "2019/04/21", "2019/04/27", "2019/04/28", "2019/05/01", "2019/05/04", "2019/05/05", "2019/05/11", "2019/05/12", "2019/05/18", "2019/05/19", "2019/05/25", "2019/05/26", "2019/06/01", "2019/06/02", "2019/06/07", "2019/06/08", "2019/06/09", "2019/06/15", "2019/06/16", "2019/06/22", "2019/06/23", "2019/06/29", "2019/06/30", "2019/07/06", "2019/07/07", "2019/07/13", "2019/07/14", "2019/07/20", "2019/07/21", "2019/07/27", "2019/07/28", "2019/08/03", "2019/08/04", "2019/08/10", "2019/08/11", "2019/08/17", "2019/08/18", "2019/08/24", "2019/08/25", "2019/08/31", "2019/09/01", "2019/09/07", "2019/09/08", "2019/09/13", "2019/09/14", "2019/09/15", "2019/09/21", "2019/09/22", "2019/09/28", "2019/10/01", "2019/10/02", "2019/10/03", "2019/10/04", "2019/10/05", "2019/10/06", "2019/10/07", "2019/10/13", "2019/10/19", "2019/10/20", "2019/10/26", "2019/10/27", "2019/11/02", "2019/11/03", "2019/11/09", "2019/11/10", "2019/11/16", "2019/11/17", "2019/11/23", "2019/11/24", "2019/11/30", "2019/12/01", "2019/12/07", "2019/12/08", "2019/12/14", "2019/12/15", "2019/12/21", "2019/12/22", "2019/12/28", "2019/12/29"];
function countWorkDay(startTime, endTime) {
if (startTime === undefined) {
startTime = new Date();
} else {
startTime = startTime.replace('年', '/').replace('月', '/').replace('日', ''); //将时间格式转化为2019/02/10的格式
}
if (endTime === undefined) {
endTime = new Date();
} else {
endTime = endTime.replace('年', '/').replace('月', '/').replace('日', ''); //将时间格式转化为2019/02/10的格式
}
var start_time = new Date(startTime);
var end_time = new Date(endTime);
var time = end_time.getTime() - start_time.getTime(); //计算两个时间的毫秒差
var days = parseInt(time / (1000 * 60 * 60 * 24)); //根据毫秒差计算得到相差天数
if (days < 0) { //实际业务需要,某个时间不能小于某个时间,否则给出错误提示
return days;
}
days += 1; // 实际业务需要计算天数(当天也算一天)
for (var i = 0; i < arr.length; i++) { //获取两个时间之间的工作日天数
var holidaytime = new Date(arr[i]).getTime();
if (holidaytime >= start_time.getTime() && holidaytime <= end_time.getTime()) { //如果两个时间之间包含节假日期就减去那天,得到需要的工作日天数
days -= 1;
}
}
return days;
}
let startTime = '2018/01/07'
let endTime = '2018/01/01'
let days = countWorkDay(startTime, endTime)
console.log('days', days);
</script>