0

I need to compare the run times of marathon runners, im having difficulty comparing the run times as they are in the following format; 00:31:26, 00:34:29 (Hours:Minutes:Seconds).

Ideally I would like to convert the whole time into minutes so that I could then use the times to create a graph.

How could I convert the race times into a number using javascript or otherwise?

Mark Taylforth
  • 187
  • 3
  • 13
  • 3
    As seconds is the smallest part that you are displaying, would it not be sensible to convert to seconds? – Xotic750 May 06 '13 at 10:30

5 Answers5

3

This example demonstrates Array.split to split the string into components, hours, minutes and seconds which are held in the variable array. It then uses parseInt to convert the component strings into an integer, which are then in turn mathematically multiplied and added together to give you a representation in seconds.

var time = "01:32:29";
var array = time.split(":");
var seconds = (parseInt(array[0], 10) * 60 * 60) + (parseInt(array[1], 10) * 60) + parseInt(array[2], 10)

console.log(seconds);

On jsfiddle

Xotic750
  • 22,914
  • 8
  • 57
  • 79
3

Same basic method, more succinctly

  '02:04:03'.split (':').reduce (function (seconds, v) {
      return +v + seconds * 60;
    }, 0) / 60;

The split creates an array, the reduce and its function calculates the time in seconds, which is finally divided by 60 to get minutes as a floating point number. The result of the above is 124.05.

HBP
  • 15,685
  • 6
  • 28
  • 34
0

First you need to convert into seconds as you have your time in hh:mm:ss. If you convert direct to minutes, you may not get exact time. You need to implement it this way :

Example :

var hms = '03:09:56';    // your input string
var a = hms.split(':'); // split it at the colons

// minutes are worth 60 seconds. Hours are worth 60 minutes.
var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]); 

//if you want hours
var minutes = (seconds)/60;

console.log(minutes);
Anil
  • 1,028
  • 9
  • 20
  • 5
    It's ok to copy code from [another answer](http://stackoverflow.com/a/9640384/502381), but if you do so you should credit the original author. – JJJ May 06 '13 at 10:34
0

Without a library, I would use something like this

convertToMinutes = function(timeString) {
    var hms = timeString.split(':');
    return Math.ceil(parseInt(hms[2])/60) + parseInt(hms[1]) + parseInt(hms[0])*60
}

demo

http://jsbin.com/awiyir/1/edit

drinchev
  • 19,201
  • 4
  • 67
  • 93
0

It's also possible to use the built in Date object. Then you can use built in functions for calculations, formatting etc.

Since Date is a date and not a simple time, I suggest aligning it with unix time. Then .getTime() will get the correct number of milliseconds.

var time1 = new Date ('01 01 70 00:31:26 UTC');
var time2 = new Date ('01 01 70 00:29:15 UTC');
var timediff=time1-time2;  // Gets the time diff in millisecs

It is a bit hackish, but it works well.

More about the Date object: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date

Atle
  • 5,299
  • 1
  • 15
  • 10