公司项目最近有个需求,要统计人员有多少天没使用工作系统。

原本只是简单地将当前时间与最后使用时间相减,得到的天数做为未使用的天数。

//法定节假日和调休日的设定
var Holiday = ["2012-01-01", "2012-01-02", "2012-01-03", "2012-01-22", "2012-01-23", "2012-01-24", "2012-01-25", "2012-01-26", "2012-01-27", "2012-01-28", "2012-04-02", "2012-04-03", "2012-04-04", "2012-04-29", "2012-04-30", "2012-05-01", "2012-06-22", "2012-06-23", "2012-06-24", "2012-09-30", "2012-10-01", "2012-10-02", "2012-10-03", "2012-10-04", "2012-10-05", "2012-10-06", "2012-10-07"];


var WeekendsOff = ["2011-12-31", "2012-01-21", "2012-01-29", "2012-03-31", "2012-04-01", "2012-04-28", "2012-09-29"];

function nearlyWeeks (mode, weekcount, end) {
/*
功能:计算当前时间(或指定时间),向前推算周数(weekcount),得到结果周的第一天的时期值;
参数:
mode -推算模式('cn'表示国人习惯【周一至周日】;'en'表示国际习惯【周日至周一】)
weekcount -表示周数(0-表示本周, 1-前一周,2-前两周,以此推算);
end -指定时间的字符串(未指定则取当前时间);
*/

if (mode == undefined) mode = "cn";
if (weekcount == undefined) weekcount = 0;
if (end != undefined)
end = new Date(new Date(end).toDateString());
else
end = new Date(new Date().toDateString());

var days = 0;
if (mode == "cn")
days = (end.getDay() == 0 ? 7 : end.getDay()) - 1;
else
days = end.getDay();

return new Date(end.getTime() - (days + weekcount * 7) * 24 * 60 * 60 * 1000);
};

function getWorkDayCount (mode, beginDay, endDay) {
/*
功能:计算一段时间内工作的天数。不包括周末和法定节假日,法定调休日为工作日,周末为周六、周日两天;
参数:
mode -推算模式('cn'表示国人习惯【周一至周日】;'en'表示国际习惯【周日至周一】)
beginDay -时间段开始日期;
endDay -时间段结束日期;
*/
var begin = new Date(beginDay.toDateString());
var end = new Date(endDay.toDateString());

//每天的毫秒总数,用于以下换算
var daytime = 24 * 60 * 60 * 1000;
//两个时间段相隔的总天数
var days = (end - begin) / daytime + 1;
//时间段起始时间所在周的第一天
var beginWeekFirstDay = nearlyWeeks(mode, 0, beginDay.getTime()).getTime();
//时间段结束时间所在周的最后天
var endWeekOverDay = nearlyWeeks(mode, 0, endDay.getTime()).getTime() + 6 * daytime;

//由beginWeekFirstDay和endWeekOverDay换算出,周末的天数
var weekEndCount = ((endWeekOverDay - beginWeekFirstDay) / daytime + 1) / 7 * 2;
//根据参数mode,调整周末天数的值
if (mode == "cn") {
if (endDay.getDay() > 0 && endDay.getDay() < 6)
weekEndCount -= 2;
else if (endDay.getDay() == 6)
weekEndCount -= 1;

if (beginDay.getDay() == 0) weekEndCount -= 1;
}
else {
if (endDay.getDay() < 6) weekEndCount -= 1;

if (beginDay.getDay() > 0) weekEndCount -= 1;
}

//根据调休设置,调整周末天数(排除调休日)
$.each(WLD.Setting.WeekendsOff, function (i, offitem) {
var itemDay = new Date(offitem.split('-')[0] + "/" + offitem.split('-')[1] + "/" + offitem.split('-')[2]);
//如果调休日在时间段区间内,且为周末时间(周六或周日),周末天数值-1
if (itemDay.getTime() >= begin.getTime() && itemDay.getTime() <= end.getTime() && (itemDay.getDay() == 0 || itemDay.getDay() == 6))
weekEndCount -= 1;
});
//根据法定假日设置,计算时间段内周末的天数(包含法定假日)
$.each(WLD.Setting.Holiday, function (i, itemHoliday) {
var itemDay = new Date(itemHoliday.split('-')[0] + "/" + itemHoliday.split('-')[1] + "/" + itemHoliday.split('-')[2]);
//如果法定假日在时间段区间内,且为工作日时间(周一至周五),周末天数值+1
if (itemDay.getTime() >= begin.getTime() && itemDay.getTime() <= end.getTime() && itemDay.getDay() > 0 && itemDay.getDay() < 6)
weekEndCount += 1;
});

//工作日 = 总天数 - 周末天数(包含法定假日并排除调休日)
return days - weekEndCount;
};


 

Logo

为武汉地区的开发者提供学习、交流和合作的平台。社区聚集了众多技术爱好者和专业人士,涵盖了多个领域,包括人工智能、大数据、云计算、区块链等。社区定期举办技术分享、培训和活动,为开发者提供更多的学习和交流机会。

更多推荐