js中计算两个日期间的工作日(只除去周六日)
程序员文章站
2022-05-18 08:01:10
...
/**
* 计算办理时长(除去周末)
*
* @param sDay(string)
* @param eDay(string)
* @returns
*/
function countWorkDay(sDay,eDay){
var sDay = new Date(Date.parse(sDay.replace(/-/g, "/")));
var eDay = new Date(Date.parse(eDay.replace(/-/g, "/")));
var s_t_w = sDay.getDay(), e_t_w = eDay.getDay();
// 总相差天数
var diffDay = (eDay - sDay) / (1000 * 60 * 60 * 24) + 1;
if(parseInt(diffDay) == 0)
return parseInt(diffDay);
// 周末天数
var weekEnds = 0;
for(var i = 0; i < diffDay; i++)
{
if(sDay.getDay() == 0 || sDay.getDay() == 6)
weekEnds ++;
sDay = sDay.valueOf();
sDay += 1000 * 60 * 60 * 24;
sDay = new Date(sDay);
}
return parseInt(diffDay - weekEnds);
}
想要除去法定节假的话,可以将所有节假日存到数据库中,然后与数据库中的日期比对。