ここにはいくつかのものがあります。まず、はるかに効率的にすでに提供さよりも、2つの日付の間の日数を取得する:
/*
Get the number of days between two dates - not inclusive.
"between" does not include the start date, so days
between Thursday and Friday is one, Thursday to Saturday
is two, and so on. Between Friday and the following Friday is 7.
e.g. getDaysBetweenDates(22-Jul-2011, 29-jul-2011) => 7.
If want inclusive dates (e.g. leave from 1/1/2011 to 30/1/2011),
use date prior to start date (i.e. 31/12/2010 to 30/1/2011).
Only calculates whole days.
*/
function getDaysBetweenDates(d0, d1) {
var msPerDay = 8.64e7;
// Copy dates so don't mess them up
var x0 = new Date(d0);
var x1 = new Date(d1);
// Set to noon - avoid DST errors
x0.setHours(12,0,0);
x1.setHours(12,0,0);
// Round to remove daylight saving errors
return Math.round((x1 - x0)/msPerDay);
}
のために調整し、その後、5を掛け、全体の週の数をうまく、「ビジネス」の日数を取得するには残りの日と週末は開始時と終了時である。開始日(休暇の典型的なもの)を含める場合は、指定日の1日前に開始日を使用します。例えば2011年7月4日から2011年7月8日までの休暇申請は、5日間の休暇です。一方
、5営業日から月4月 - 2011は、通常、次の月曜日、11月-2011ですが、金曜の8月 - 2011 COBかもしれません。そのようなもののルールは、地域の慣習、法的判例、契約の合意などによって異なる場合があります。
また、このような単純なアルゴリズムではほとんどの場合十分ではありません。祝日は物事を混乱させますが、別の場所では別の作業週もあります。土曜日〜水曜日は木曜日と金曜日が「週末」です。他の場所では、土曜日は仕事の日(週6日)です。また、祝日は週末になる可能性があります。
/*
Simple algorithm: each week has 5 business days: Monday
to Friday inclusive. Saturday and Sunday are non-working
days.
*/
function getBusinessDaysBetweenDates(d0, d1) {
var days = getDaysBetweenDates(d0, d1);
var bWeeks = (days/7)|0;
var rDays = days%7;
var startDay = d0.getDay();
var endDay = d1.getDay();
// If not whole weeks, adjust for weekends
if (rDays) {
// Adjust for start on weekend, end any day
if (startDay == 6) {
rDays -= 1;
} else if(startDay == 5) {
rDays -= (rDays == 1)? 1 : 2;
// Adjust for start on business day
// and end on weekend
} else if (endDay == 6) {
rDays -= 1;
// Adjust for going over whole weekend
} else if (endDay < startDay) {
rDays -= (rDays == 1)? 1 : 2;
}
}
return bWeeks * 5 + rDays;
}
あなたは月曜日から金曜日までの日に落ちる祝日を含めたい場合は、あなたが祝日のカレンダーを使用し、対象の日付の間の営業日にどのように多くの秋を返す別の関数が必要になります。また、特定の産業、職業、宗教などの休日があるかもしれません。
祝祭日はどうですか? – RobG
@RobG - はい - 公休日もあります。 –
ここで:http:// stackoverflow。com/questions/59934/national-holiday-web-service – mplungjan