计算Javascript中的小时差异,但仅限于某些小时

时间:2012-04-27 15:03:34

标签: javascript date time

不确定描述这个的最佳方式,但我需要计算小时数差异(向下舍入)但仅在晚上8点到早上6点之间(或者更确切地说是20:00 - 06:00)!

例如:

22:00 - 04:00 (6 hours)
02:40 - 10:20 (4 hours)
20:00 - 06:00 (10 hours)

不幸的是,我需要处理确切的日期,因为有些日期会持续多天 - 只是为了增加混乱,我还需要完全排除某些日期以用于银行假期(我有一个数组中的列表)但绝对不知道如何实现这一点 - 非常欢迎任何建议,谢谢

2 个答案:

答案 0 :(得分:4)

关闭示例中的输入:

// According to your example, your inputs are strings...
// t1 = "22:00"
// t2 = "04:00";

function hoursDiff(t1, t2){

   // Parse out the times, using radix 10 to
   // avoid octal edge cases ("08:00" & "09:00")
   var time1   = parseInt( t1, 10 );
   var time2   = parseInt( t2, 10 );
   var hours   = 0;

   while ( time1 !== time2 ){
      time1++;
      hours++;

      // If we've passed midnight, reset
      // to 01:00AM
      if ( time1 === 25 ){
         time1 = 1;
      }  
   }

   return hours;
}

答案 1 :(得分:2)

好的,这是一个很好的谜题。这真的花了我太多时间,但很有趣。

下面的完整工作代码(jsfiddle here):

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)});