I've got a function that takes in milliseconds and returns a formatted string of larger units of time. For example, if you pass in 74600 milliseconds, it returns the string "1:14.600".
My issue right now is I want it to only return the first two digits of the milliseconds place, but I'm not exactly sure how to do that. I was thinking about truncating the string, but I think you can only do that based on length, and if there were two digits in the minutes place it would throw that off.
Here's my timeFormatter() function:
function timeFormatter(s) {
var ms = s % 1000;
s = (s - ms) / 1000;
var secs = s % 60;
s = (s - secs) / 60;
var mins = s % 60;
var hrs = (s - mins) / 60;
if (hrs > 0) {
} else {
if (mins > 0) {
if (secs < 9) {
if (ms < 99) {
return mins + ':0' + secs + '.0' + ms;
} else {
return mins + ':0' + secs + '.' + ms;
}
} else {
if (ms < 99) {
return mins + ':' + secs + '.0' + ms;
} else {
return mins + ':' + secs + '.' + ms;
}
}
} else {
if (ms < 99) {
return secs + '.0' + ms;
} else {
return secs + '.' + ms;
}
}
}
}
I know there's that big gross if statement in there, I'm not really concerned with simplifying that yet.
Thanks!
EDIT: I should add that with smaller strings (under a minute) to fix this problem, I just used toFixed(2) to keep it at only two decimal places, so this problem only exists with larger strings (over a minute).