Created
March 1, 2023 23:38
-
-
Save StoneyEagle/5a39d9a8d731880d94292391339da286 to your computer and use it in GitHub Desktop.
Seconds to human time
This file contains 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
humanTime (time: string | number) { | |
time = parseInt(time as string, 10); | |
let days: any = parseInt(`${(time / (3600 * 24))}`, 10); | |
let hours: any = this.pad(parseInt(`${(time % 86400) / 3600}`, 10), 2); | |
let minutes: any = parseInt(`${(time % 3600) / 60}`, 10); | |
let seconds: any = parseInt(`${time % 60}`, 10); | |
if (`${minutes}`.length === 1) { | |
minutes = `0${minutes}`; | |
} | |
if (`${seconds}`.length === 1) { | |
seconds = `0${seconds}`; | |
} | |
if (days === 0) { | |
days = ''; | |
} else { | |
days = `${days}:`; | |
} | |
if (hours === 0) { | |
hours = '00:'; | |
} else { | |
hours = `${hours}:`; | |
} | |
if (minutes === 0) { | |
minutes = '00:'; | |
} else { | |
minutes = `${minutes}:`; | |
} | |
if (hours == '00:' && days == '') { | |
hours = ''; | |
} | |
const current = days + hours + minutes + seconds; | |
return current.replace('NaN:NaN:NaN:NaN', '00:00'); | |
} | |
pad(number: string | number, places = 2) { | |
if (typeof number !== 'undefined') { | |
const zero = places - number.toString().length + 1; | |
return Array(+(zero > 0 && zero)).join('0') + number; | |
} | |
return ''; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment