Last active
July 28, 2022 18:22
-
-
Save itzarty/8269f31d61980ac0df4d6bef82ac104e to your computer and use it in GitHub Desktop.
Simple function to format seconds into human-readable string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Requires prototype found at https://gist.github.com/ItzArty/96e7439a63ac3800335ecb72a63d255c | |
function formatTime( seconds, minutes, hours, forceHours ) { | |
if( !seconds ) seconds = 0; | |
if( !minutes ) minutes = 0; | |
if( !hours ) hours = 0; | |
if( !forceHours ) forceHours = false; | |
if( seconds / 60 >= 1 ) { | |
var add = Math.floor( seconds / 60 ); | |
minutes += add; | |
seconds = seconds - ( 60 * add ); | |
} | |
if( minutes / 60 >= 1 ) { | |
var add = Math.floor( minutes / 60 ); | |
hours += add; | |
minutes = minutes - ( 60 * add ); | |
} | |
var formatted = ''; | |
if( hours > 0 || forceHours == true ) { | |
formatted += hours + ':' + minutes.placeFormat( 2 ); | |
}else{ | |
formatted += minutes; | |
} | |
formatted += ':' + Math.round( seconds ).placeFormat( 2 ); | |
return formatted; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment