2

I'm working in javascript - and I have a key-value object, that holds in his "value" field this: 9.00 _ 12.30. This represents a start and end time for a certain session, and I would like to convert it to a duration (meaning 9.00 _ 12.30 --> 3:30 or 3.5). How can I do it? Thanks!

Bramat
  • 979
  • 4
  • 24
  • 40

2 Answers2

2

You can use split function:

t = '9.00_12.30'.split('_');
t0 = t[0].split('.');
t1 = t[1].split('.');

And get interval:

interval = t1[0]-t0[0]+t1[1]/60-t0[1]/60;
Max Lipsky
  • 1,774
  • 1
  • 18
  • 29
2

var t = '9.00 _ 12.30'.split(/\W/).map(parseFloat), h, m, r;

t = (t[3] * 60 + t[4]) - (t[0] * 60 + t[1]); // convert `time` values to minutes
m = t % 60;                                  // get minutes
h = (t  - m) / 60;                           // get hours
r = h + ':' + (m < 10 ? '0' + m : m);        // the resulting string

alert(r);
Vidul
  • 10,128
  • 2
  • 18
  • 20