【问题标题】:How do I calculate hours between two 'HHmm' in Javascript?如何计算 Javascript 中两个“HHmm”之间的小时数?
【发布时间】:2014-11-01 21:02:07
【问题描述】:

是否可以计算两个 'HHmm' 字符串之间的分钟数或小时数。

Javascript 示例:

var now = 2050,
    next = 0850,
    minutesUntilNext = *now until then code*,
    hoursUntilNext = *minutes to hours code*;

我无权访问实际的 Date 对象,这就是为什么这有点困难。 但是,我正在使用 moment.js,所以如果您对如何在此使用它有建议 情况,那就完美了。

【问题讨论】:

  • “确定”和“否”。 “当然”,因为您可以构造两个通用日期和use normal moment.js code。 “不”,因为哪个更早?很容易假设 20h 在 8h 之后,但是 23h 和 1h 呢?是从第 X 天到第 X+1 天相差 2 小时,还是在同一天相差 22 小时?
  • 如果小于现在且大于 0000,我们必须假设 next 总是第二天,但我们仍然不知道它是未来的一天还是几天。
  • 如果您可以以任何方式访问 now = date('u') ,这将不是问题。
  • @Mike'Pomax'Kamermans:我看不出问题所在。 “下一步”是明确的。

标签: javascript momentjs


【解决方案1】:

这对于一些基本的除法和减法来说非常简单:

// http://stackoverflow.com/a/10075654/560648
function padDigits(number, digits) {
    return Array(Math.max(digits - String(number).length + 1, 0)).join(0) + number;
}

/**
 * Given two times in HHMM format, returns the timespan between them
 * in HH:MM format.
 * 
 * %next is assumed to come later than %now, even if its absolute value
 * is lower (so, the next day).
 */
function compareTimes(now, next) {

   // Perform hours validation before we potentially add 24 hours (see below)
   if (now >= 2400 || next >= 2400)
      throw "Hours out of bounds";

   // If next is "earlier" than now, it's supposed to be tomorrow;
   // adding 24 hours handles that immediately
   if (next < now) next += 2400;

   // Split inputs into hours and minutes components
   now  = [parseInt(now  / 100, 10), now  % 100];
   next = [parseInt(next / 100, 10), next % 100];

   // Finally, validate the minutes
   if (now[1] >= 60 || next[1] >= 60)
      throw "Minutes out of bounds";

   // Perform the comparisons
   var minutesUntilNext = next[1] - now[1];
   var hoursUntilNext   = next[0] - now[0];

   // And return the result
   return padDigits(hoursUntilNext, 2) + ':' + padDigits(minutesUntilNext, 2);
}

console.log(doThisThing(2050, 0850));  // 12:00
console.log(doThisThing(2300, 0145));  // 02:45
//console.log(doThisThing(2500, 0000));  // error
//console.log(doThisThing(2460, 0000));  // error

【讨论】:

  • 不慎被否决,请编辑您的答案以撤消投票
【解决方案2】:

这只是一道数学题……

它可能看起来像这样:

var now = 2230;
var then = 529;

function calculateMinutesDifference(now, then){
  var nowMinutes = getMinutes(now);
  var thenMinutes = getMinutes(then);
  return thenMinutes >= nowMinutes
         ? thenMinutes - nowMinutes
         : (thenMinutes+60) - nowMinutes;
}


function calculateHoursDifference(now, then){
  var nowHours = getHours(now);
  var thenHours = getHours(then);
  return then >= now
  ? thenHours - nowHours
  : (thenHours+(calculateMinutesDifference(now,then) == 0 ? 24 : 23)) - nowHours;
}

function getMinutes(time){
  return time % 100;
}

function getHours(time){
  return (time - getMinutes(time))/100;  
}

alert("minutes difference: " + calculateMinutesDifference(now, then));
alert("hours difference: " + calculateHoursDifference(now, then));

当小时

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多