2017-02-17 7 views
0

私は2つの日付間のビジネス時間を分単位で計算しようとしています。営業時間は午前8時から午後6時(東部標準時間(夏時間の場合はEDT))と定義されています。夕方と週末を除いた日数を計算しますか?

私は時間でこれを実行し、この答えを、見つけましたが、分に変換する方法がわからないよ、と文句を言わない私のタイムゾーンを確保するために台無しにされました:

https://stackoverflow.com/a/11092865/104998

function isHoliday(/*Date*/ date) { 
 
    for (var i = 0; i < holidays.length; i++) { 
 
    if (holidays[i].getTime() == date.getTime()) { 
 
     return true; 
 
    } 
 
    } 
 
    
 
    return false; 
 
} 
 

 
function diffHours(/*Date*/ d1, /*Date*/ d2) { 
 
    var date1 = new Date(d1.getUTCFullYear() + "-" + (d1.getUTCMonth() + 1) + "-" + d1.getUTCDate() + " UTC"); 
 
    var date2 = new Date(d2.getUTCFullYear() + "-" + (d2.getUTCMonth() + 1) + "-" + d2.getUTCDate() + " UTC"); 
 

 
    var sum = 0; 
 
    var oneday = 24 * 3600 * 1000; 
 
    var hours, date; 
 

 
    // first day 
 
    if (!isHoliday(date1)) { 
 
    // decrease by a whole day first (will be added later) 
 
    sum -= 10; 
 

 
    // add real hours 
 
    hours = d1.getUTCHours() + d1.getUTCMinutes()/60; 
 
    if (hours <= 6) { 
 
     sum += 10 - hours; 
 
    } else if (hours <= 20) { 
 
     sum += 4; 
 
    } else { 
 
     sum += 24 - hours; 
 
    } 
 
    } 
 

 
    // last day 
 
    if (!isHoliday(date2)) { 
 
    // decrease by a whole day first (will be added later) 
 
    sum -= 10; 
 

 
    // add real hours 
 
    hours = d2.getUTCHours() + d2.getUTCMinutes()/60; 
 
    if (hours <= 6) { 
 
     sum += hours; 
 
    } else if (hours <= 20) { 
 
     sum += 6; 
 
    } else { 
 
     sum += hours - 14; 
 
    } 
 
    } 
 

 
    // whole days 
 
    while (date1 <= date2) { 
 
    if (!isHoliday(date1)) { 
 
     sum += 10; 
 
    } 
 

 
    // increase date by 1 day 
 
    date1.setTime(date1.getTime() + oneday); 
 
    } 
 

 
    return Math.floor(sum); 
 
} 
 

 
// ============== 
 
// examples below 
 
// -------------- 
 

 
// array of Dates (in UTC) to skip 
 
var holidays = [ 
 
    new Date("2012-01-04 UTC"), 
 
]; 
 

 
for (var i = 0; i < holidays.length; i++) { 
 
    console.log('holiday: ', holidays[i].toUTCString()); 
 
} 
 

 
a = new Date("2012-01-01 12:00 UTC"); 
 
b = new Date("2012-01-02 12:00 UTC"); 
 
c = new Date("2012-01-02 22:00 UTC"); 
 
d = new Date("2012-01-03 07:00 UTC"); 
 
e = new Date("2012-01-05 12:00 UTC"); 
 

 
console.log({ 
 
    d1: a.toUTCString(), 
 
    d2: b.toUTCString(), 
 
    hours: diffHours(a, b) 
 
}); 
 
console.log({ 
 
    d1: b.toUTCString(), 
 
    d2: c.toUTCString(), 
 
    hours: diffHours(b, c) 
 
}); 
 
console.log({ 
 
    d1: c.toUTCString(), 
 
    d2: d.toUTCString(), 
 
    hours: diffHours(c, d) 
 
}); 
 
console.log({ 
 
    d1: d.toUTCString(), 
 
    d2: e.toUTCString(), 
 
    hours: diffHours(d, e) 
 
});

ご協力いただければ幸いです。ここで

+0

がなければならない時間を掛けby 60 –

+0

チップGeorgeに感謝します:) –

答えて

0

が、私はこれを行うために思い付いたクラスであり、分に変換するには良い方法(これはnodejs掘り下げる初めてです)

var dateFuncs = DateFuncs.prototype; 
 

 
function DateFuncs() { 
 

 
} 
 

 

 
DateFuncs.prototype.isWeekend = function(pDate) { 
 
    return (pDate.getDay() == 0 || pDate.getDay() == 6); 
 
} 
 

 
DateFuncs.prototype.isBusinessMinute = function(pDate) { 
 
    var hours = pDate.getHours() + pDate.getMinutes()/60; 
 
    return (hours >= 8 && hours < 18); //business hours are 8AM-6PM 
 
} 
 

 
DateFuncs.prototype.addMinutes = function(date, minutes) { 
 
    return new Date(date.getTime() + minutes*60000); 
 
} 
 

 
DateFuncs.prototype.diffBusinessMins = function(/*Date*/ startDate, /*Date*/ endDate) { 
 
    var minutesDiff = 0; 
 
    startDate.setSeconds(0,0); 
 
    endDate.setSeconds(0,0); 
 

 
    var curDate = new Date(startDate.getTime()); 
 

 
    while(curDate.getTime() != endDate.getTime()) 
 
    { 
 
    if(!this.isWeekend(curDate) && this.isBusinessMinute(curDate)) 
 
    { 
 
     minutesDiff += 1; 
 
    } 
 

 
    curDate = this.addMinutes(curDate, 1); 
 
    } 
 

 
    return minutesDiff; 
 
} 
 

 

 

 
module.exports = DateFuncs;

関連する問題