I want to convert a duration of time, i.e., number of seconds to colon-separated time string (hh:mm:ss)
I found some useful answers here but they all talk about converting to x hours and x minutes format.
So is there a tiny snippet that does this in jQuery or just raw JavaScript?
I like the first answer. There some optimisations:
source data is a Number. additional calculations is not needed.
much excess computing
Result code:
Variation on a theme. Handles single digit seconds a little differently
I liked Webjins answer the most, so i extended it to display days with a d suffix, made display conditional and included a s suffix on plain seconds:
returns "3d 16:32:12" or "16:32:12" or "32:12" or "12s"
I'm personally prefer the leading unit (days, hours, minutes) without leading zeros. But seconds should always be leaded by minutes (0:13), this presentation is easily considered as 'duration', without further explanation (marking as min, sec(s), etc.), usable in various languages (internationalization).
//imitating the server side (.net, C#) duration formatting like:
Non-prototype version of toHHMMSS:
Here's how I did it. It seems to work fairly well, and it's extremely compact. (It uses a lot of ternary operators, though)
...and for formatting strings...