日期:2014-05-16  浏览次数:20549 次

]JS端计算一段时间内工作日的天数,排除周末和法定节假日,同时考虑到调休日

/[原创]JS端计算一段时间内工作日的天数,排除周末和法定节假日,同时考虑到调休日

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

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

结果,领导说这样不行,需要计算得精确些,于是上网搜到不少解决方案,不是通过循环解决,就是都不符合我们的实际情况,便自己写一个。

项目是jQuery的,所以循环语句也需要自己改一下。希望对同样需求的兄弟姐妹们有所帮助,也希望大家多多拍砖,提点意见哈!/法定节假日和调休日的设定

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;

? ? });

? ? //根据法定假日设置