[原创]JS侧は一时间内の平日の日数を计算して、周末と法定の祝日を排除して、同时に休日を考虑して...

7462 ワード

会社のプロジェクトは最近需要があって、人員が何日ワークシステムを使用していないかを統計します.
本来は単純に現在の時間と最後の使用時間を減算し、得られた日数を未使用の日数とする.
その結果、指導者はこのようにだめだと言って、計算を正確にしなければならないと言って、そこでインターネットで多くの解決策を探して、循環を通じて解決するのではなくて、すべて私たちの実際の状況に合わないで、自分で1つ書きます.
プロジェクトは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;
});
// , ( )
$.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;
};

 
転載先:https://www.cnblogs.com/w3live/archive/2012/02/10/2345461.html