javascript – Can anyone write this simple function better?


I am making an hours calculator app. It takes a start time, end time and time taken for lunch.

For the start & end time it takes a 4 digit hh:mm time. eg. 10:20.

I have made a function which converts the time into a decimal,
so 10:20 = 10.33. The function works but I feel it looks a little heavy and wondered if anyone has any suggestions of how I could make it better…

Any help would be appreciated.

const minuteConverter = time => {
    let h = Number(time.split(':')(0));
    let m = Math.round((1 / 60 * (Number(time.split(':')(1))) + Number.EPSILON) * 100) / 100;
    let mConverted = Number(m.toString().split('.')(1))
    return Number(`${h}.${mConverted}`)
};
console.log(minuteConverter('10.20'))

The time must be output as a number to 2 decimal places.
e.g.

  • ’10:20′ >> 10.33
  • ‘9:45’ >> 9.75
  • ’15:33′ >> 15.55