Javascript | Function | Translate Seconds into days, hours, minutes and seconds
Hello there! Have you ever found yourself needing to translate seconds into a more human-readable format that includes days, hours, minutes, and seconds? Well, worry not, because in this article, I’m going to introduce you to a nifty JavaScript function that performs exactly that task. This function has proven to be incredibly useful to me, and I believe it could be a lifesaver for anyone facing a similar challenge.
The Quest for Simplicity and Utility
Before we delve into the details, let me clarify that this method wasn’t originally devised by me. I stumbled upon it while scouring the vast expanse of the internet. Since it proved to be immensely practical for my needs, I decided it was only fair to share it here, benefiting others who might be searching for a similar solution.
The JavaScript Function: Seconds to Days, Hours, Minutes, and Seconds
The function that I’m about to introduce you to is called secondsToDhms
. It’s a relatively compact piece of code that takes a seconds value as input and transforms it into a formatted string that represents the equivalent time in days, hours, minutes, and seconds. Let’s take a closer look at the code:
Function Code
function secondsToDhms(seconds) {
seconds = Number(seconds)
var d = Math.floor(seconds / (3600 * 24))
var h = Math.floor((seconds % (3600 * 24)) / 3600)
var m = Math.floor((seconds % 3600) / 60)
var s = Math.floor(seconds % 60)
// console.log(d, h, m, s)var dDisplay = d > 0 ? d + (d == 1 ? " day, " : " days, ") : ""
var hDisplay = h > 0 ? h + (h == 1 ? " hour, " : " hours, ") : ""
var mDisplay = m > 0 ? m + (m == 1 ? " minute, " : " minutes, ") : ""
var sDisplay = s > 0 ? s + (s == 1 ? " second" : " seconds") : ""
return dDisplay + hDisplay + mDisplay + sDisplay
}
Testing and output
console.log(secondsToDhms(1000));
// "16 minutes, 40 seconds"console.log(secondsToDhms(360));
// "6 minutes, "console.log(secondsToDhms(12345678));
// "142 days, 21 hours, 21 minutes, 18 seconds"console.log(secondsToDhms(3600));
// "1 hour, "
Giving Credit Where It’s Due
I want to extend my gratitude to the brilliant mind who crafted this function. Though their identity remains a mystery, their contribution has undeniably made our lives easier.
In Conclusion
There you have it—a straightforward yet incredibly handy JavaScript function that transforms seconds into a human-readable representation of days, hours, minutes, and seconds. Whether you’re building applications, analyzing data, or just satisfying your curiosity, this function is a testament to the collaborative spirit of the coding community. So go ahead, embrace the elegance of this solution, and happy coding!
You May also like:
Javascript | Function | Translate Seconds into days, hours, minutes and seconds
Hi would you mind stating which blog platform you’re working with?
I’m planning to start my own blog in the near future but I’m having a difficult time making a decision between BlogEngine/Wordpress/B2evolution and Drupal.
The reason I ask is because your layout seems different then most blogs and I’m looking for something unique.
P.S Apologies for getting off-topic but I had to ask!
I am using worpress and the template is “kenta yoga coach”