使用递归函数减去两个HH:MM字符串

时间:2014-01-21 23:51:39

标签: javascript time

我一直试图让this工作,我发现它here并根据我的需要进行调整。此时它到达预期值,但是返回时不会返回函数外部。我试过调试它,但似乎无法找到/理解问题。

该函数应该以“hh:mm”格式减去两个字符串,并以相同的格式返回一个字符串。其中一个字符串由用户提供,我们将在此截止日期之前减去提供服务所需的时间。

这是我用来跟踪值的console.log()语句的代码:

function subtractMinutes(time, minsToSubtract) {
    /* Converts "hh:mm" format to a total in minutes */
    function toMinutes (hh) {
        //if in the recursion it will be an int instead of a string
        if (hh < 0) {
          return Math.abs(hh);
        }
        hh = hh.split(':');
        return (parseInt(hh[0], 10) * 60) + parseInt(hh[1], 10);
    }

    /* Converts total in minutes to "hh:mm" format */
    function toText (m) {
        var minutes = m % 60;
        var hours = Math.floor(m / 60);

        minutes = (minutes < 10 ? '0' : '') + minutes;
        hours = (hours < 10 ? '0' : '') + hours;

        return hours + ':' + minutes;
    }
    console.log('time = '+time); //tracking values
    console.log('minsToSubtract = '+minsToSubtract); //tracking values

    time = toMinutes(time);
    console.log('time toMinutes = '+time); //tracking values
    minsToSubtract = toMinutes(minsToSubtract);
    console.log('minsToSubtract toMinutes = '+minsToSubtract); //tracking values

    var diff = time - minsToSubtract;
    console.log('diff = '+diff); //tracking values

    //if in recursion it will have to account for 24h/day instead of going to negative values
    if (diff < 0) {
      subtractMinutes("24:00", diff);
    } 
    //end of recursion when diff>0 and the result may be returned
    else {
      console.log('diff = '+diff); //tracking values
      var result = toText(diff);
      console.log('result = '+result); //tracking values
      return result; //at the end this value is correct, after this point it becomes "undefined"
    }
}
var result = subtractMinutes("0:35", "01:00");
console.log(result);
console.log('---------------------');

我们非常欢迎任何建议,但不会考虑包/插件/库。

----- ----- EDIT

鉴于rlemon's answer我尝试了两种不同的方法来尝试对其进行排序,但它没有用:

    if (diff < 0) {
      subtractMinutes("24:00", diff);
    } 
    //removed the else part of the statement as suggested 
    console.log('diff = '+diff); //tracking values
    var result = toText(diff);
    console.log('result = '+result); //tracking values
    return result;
}

这不再返回undefined,但是在递归解析正确的值后调用toText()函数,从而尝试再次转换该值,返回类似{{ 1}}。

所以我想我已经理解了这个问题,我试图用另一种方式解决这个问题并做了以下事情:

"0-1:0-25"

我想建议在函数的最后一个结果上放置一个断点,看看为它分配了什么值以及取消定义该值的实际情况。

2 个答案:

答案 0 :(得分:2)

在第一次函数调用之后没有返回,因此返回'undefined' 以这个简化的例子为例:

function foo( bar ) {
  if( bar ) {
    foo(false); // okay.. so you hit this and jump out of the if statement.
    // now you think because the return is going to be triggered in the next
    // pass that it will be the end result, but it isn't.
  } else {
    return 1;
  }
  // no return, so return undefined
}
foo(true)

在'递归'中更改subtractMinutes("24:00", diff); if语句到return subtractMinutes("24:00", diff);应解决问题,因为现在它不会在第一次调用时终止。

请参阅演示:http://jsfiddle.net/rlemon/2YgyJ/

作为附注:现在你不需要在那里有一个'else'语句,你可以完全省略它,只需在if:

之后运行代码
//if in recursion it will have to account for 24h/day instead of going to negative values
if (diff < 0) {
    return subtractMinutes("24:00", diff); // because of the return here, the code below is not hit until the if condition returns false
}
//end of recursion when diff>0 and the result may be returned
console.log('diff = ' + diff); //tracking values
var result = toText(diff);
console.log('result = ' + result); //tracking values
return result; 

答案 1 :(得分:1)

代码的逻辑对我来说似乎很复杂。如果减法的结果是<1,则不需要递归。 0,简单地加1,440分钟(24小时)。事情应该尽可能简单,如果不是更简单的话。

e.g。

var subtractMins = (function() {

  // Pad numbers < 10 with a leading zero (returns string)
  function z(n) {
    return (n<10? '0':'') + n;
  }

  // Convert hh:mm to minutes (returns number)
  function t2m(t) {
    var b = t.split(':');
    return b[0] * 60 + +b[1];
  }

  // Convert minutes to hh:mm (returns string)
  function m2t(m) {
    return z((m/60 | 0)) + ':' + z(m%60);
  }

  // Return t (time) minus m (minutes) as string
  // t and m are strings in hh:mm format
  // Uses 24 clock so 00:00 - 00:10 => 23:50
  return function(t, m) {
    var r = t2m(t) - (t2m(m) % 1440);
    return m2t(r<0? r + 1440 : r);
  };
}());

alert(subtractMins('00:00','00:10')); // 23:50
alert(subtractMins('01:00','00:10')); // 00:50
alert(subtractMins('01:00','51:10')); // 21:50

修改

修改,使结果在24小时范围内。

相关问题