Skip to content

Instantly share code, notes, and snippets.

@pawiromitchel
Created November 2, 2020 16:06
Show Gist options
  • Save pawiromitchel/8fccb56308e74fee37f4cbdb4eeb018c to your computer and use it in GitHub Desktop.
Save pawiromitchel/8fccb56308e74fee37f4cbdb4eeb018c to your computer and use it in GitHub Desktop.
JS: hh:mm:ss to hour decimal
function timeStringToFloat(time) {
var hoursMinutes = time.split(/[.:]/);
var hours = parseInt(hoursMinutes[0], 10);
var minutes = hoursMinutes[1] ? parseInt(hoursMinutes[1], 10) : 0;
var seconds = hoursMinutes[2] ? parseInt(hoursMinutes[2], 10) : 0;
return parseFloat((hours + (minutes / 60) + (seconds / 3600)).toFixed(2));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment