0

I have two <input type="time">. By default, each input collects a time value as a string. For example, "08:30".

How can I convert this string into an object which would then enable computation? Is it possible to avoid involving the use of date in this approach?

In the end, I would like to compute the difference between two time strings and then return the result in minutes. For example, the expected return value of08:00 and 09:00 would be 60 minutes.

hyang123
  • 1,208
  • 1
  • 13
  • 32
  • 1
    I'd use Date instances for some arbitrary date, though strictly speaking (due to things like Daylight Savings shifts) the difference between two time expressions does depend on what day you're talking about. – Pointy Mar 20 '17 at 15:57
  • You could create your own object and split the string on the `:` and convert the values to numbers, also depends on if your time is 24 hours or 12 hours – George Mar 20 '17 at 15:58
  • why won't you use `date`if I may ask? Is the use of a library like momentjs okay? Everything else would result in re-inventing the wheel and MUCH more work on the edgecases calculating times imho. – Dominik Mar 20 '17 at 16:03
  • Possible duplicate of [How do I calculate the time difference between strings in Javascript](http://stackoverflow.com/questions/17157337/how-do-i-calculate-the-time-difference-between-strings-in-javascript) – Heretic Monkey Mar 20 '17 at 16:14
  • Hmmm... Are you sure the difference isn't 23 hours? Not sure what I mean? What if I asked you for the different between `23:30` and `00:30`? I think the only thing one could agree on is if the times are exactly 12 hours apart `09:00` to `03:00` is 12 hours no matter which side you look at it from. The point is, you need to know which value comes first, because the time could easily cross midnight. – Matt Johnson-Pint Mar 20 '17 at 16:24

3 Answers3

2

Just do it as if you only had pen and paper:

  • 12:45 => 12 × 60 + 45 = 765 minutes
  • 08:30 => 8 × 60 + 30 = 510 minutes
  • 765 - 510 = 255
  • Integer division: 255 / 60 = 4 hours
  • Remainer: 255 - 60 × 4 = 15 minutes
  • Result: 04:15

You can parse from string using regular expressions:

var parts = "08:45".match(/^(\d+):(\d+)$/);
console.log(+parts[1], +parts[2], +parts[1] * 60 + +parts[2]);

... and formatting back to string should not be very difficult either.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
0

Assuming you want to use a 24h clock:

function minutesBetween (a, b) {
  return Math.abs(toMinutes(b) - toMinutes(a))
}

function toMinutes (time) {
  time = /^(\d{1,2}):(\d{2})$/.exec(time)
  return time[1]*60 + +time[2]
}

console.log(minutesBetween('8:30', '9:30')) //=> 60
gyre
  • 16,369
  • 3
  • 37
  • 47
0

Generally speaking I whould suggest using Date insted of using custom functions, but just for your case this is the example function:

const timeStart = "8:00:00";
const timeEnd   = "8:10:00";
//Handles only time with format in hh:mm or hh:mm:ss
//For more complicated cases use Date 
function diff(start, end) {
    const startMinutes = getMinutes(start);
    const endMinutes   = getMinutes(end);
    return endMinutes - startMinutes
}

function getMinutes(strTime) {
    const time = strTime.split(':');
    return (time[0] * 60 + time[1]*1);
}

alert(diff(timeStart, timeEnd));

Note that this function is not responsible for validation of the time difference only the computation. You should validate you input

melanholly
  • 762
  • 1
  • 9
  • 20