0

Is it possible to calculate the number of minutes or hours between two 'HHmm' strings.

Javascript example:

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

I do not have access to the actual Date objects which is why this is slightly harder. I am however using moment.js so if you have suggestions for how to use that in this situation, that would be perfect.

Omair Vaiyani
  • 552
  • 5
  • 27
  • 3
    "sure" and "no". "Sure" because you can just construct two generic dates and [use normal moment.js code](http://stackoverflow.com/questions/10018575/moment-js-diff-between-utc-dates). "no", because which one is earlier? It's sure easy to assume 20h comes after 8h, but what about 23h and 1h? is that a 2 hour difference from day X to day X+1, or a 22 hour difference on the same day? – Mike 'Pomax' Kamermans Nov 01 '14 at 21:10
  • We'd have to assume that next is always the next day if less than now and greater than 0000, but we still don't know if it is just one day in the future or several days. – Gary Hayes Nov 01 '14 at 21:14
  • if you can access now = date('u') in any way shape or form, this would not be an issue. – Gary Hayes Nov 01 '14 at 21:16
  • @Mike'Pomax'Kamermans: I don't see the problem. "Next" is unambiguous. – Lightness Races in Orbit Nov 01 '14 at 21:24

2 Answers2

1

This is pretty trivial with some basic division and subtraction:

// 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
Lightness Races in Orbit
  • 358,771
  • 68
  • 593
  • 989
0

It's just a math problem...

It could look like this:

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

Remember not to use leading 0 when you have hour < 10, becouse then javascript will think it's not decimal number.

Bartek Andrzejczak
  • 1,152
  • 2
  • 11
  • 25