Created
August 4, 2020 06:19
-
-
Save patheticGeek/f196e03233b6b1d6887dd8aec0668aa0 to your computer and use it in GitHub Desktop.
Converts time from milliseconds to human readable format
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
function humanTime(ms) { | |
let seconds = ms / 1000; | |
let result = ""; | |
const days = Math.floor((seconds % 31536000) / 86400); | |
if (days > 0) result += `${days}d `; | |
const hours = Math.floor(((seconds % 31536000) % 86400) / 3600); | |
if (hours > 0) result += `${hours}h `; | |
const minutes = Math.floor((((seconds % 31536000) % 86400) % 3600) / 60); | |
if (minutes > 0) result += `${minutes}m `; | |
seconds = ((((seconds % 31536000) % 86400) % 3600) % 60).toFixed(0); | |
if (seconds > 0) result += `${seconds}s`; | |
if (result === "") result += "0s"; | |
return result; | |
} | |
module.exports = humanTime; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment