【问题标题】:Calculating hour difference in Javascript, but only certain hours计算 Javascript 中的小时差,但只有某些小时
【发布时间】:2012-04-27 15:03:33
【问题描述】:

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

例如:

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

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

【问题讨论】:

  • 我们可以查看您输入数据的样本吗?您是在使用完整的日期,还是只使用时间?
  • 为什么你的第三个例子有差异(10小时),它必须是(2小时)作为你的第二个例子。

标签: javascript date time


【解决方案1】:

只是让您的示例中的输入看起来像这样:

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

【讨论】:

  • 非常感谢,与我提出的解决方案相比,这看起来非常简单(以一种很好的方式!),但我觉得它可能会起作用!我一回到工作岗位就会尝试实施它,非常感谢:)
  • 太糟糕了,这个答案不正确。试试parseInt("08:00"),它会返回0。当字符串以零开头时,parseInt 默认将其解释为八进制数。这适用于 00-07,但 08 和 09 无效。您需要使用parseInt(t, 10),但这仍然无法正确回答您的问题。
  • @Bart:你说得很对。我已经更新了帖子以说明八进制边缘情况。我应该一直这样做。很棒的收获!
【解决方案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)});

【讨论】:

  • 非常感谢这个 Bart,不幸的是,我已经根据上面的 ajax81 代码提出了类似的解决方案,所以我不确定该给谁赏金!希望我可以拆分它,但是由于您的答案正是我在这种情况下所需要的,所以我会给他正确的答案并给您赏金,再次感谢你们俩的出色解决方案!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-02
  • 1970-01-01
相关资源
最近更新 更多