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?
You can use it now like:
Working snippet:
I think performance wise this is by far the fastest:
This is how i did it
timeFromSecs(22341938) will return '258d 14h 5m 38s'
A regular expression can be used to match the time substring in the string returned from the
toString()
method of the Date object, which is formatted as follows: "Thu Jul 05 2012 02:45:12 GMT+0100 (GMT Daylight Time)". Note that this solution uses the time since the epoch: midnight of January 1, 1970. This solution can be a one-liner, though splitting it up makes it much easier to understand.It's pretty easy,
Using the amazing moment.js library:
This allows you to specify any duration be it hours, minutes, seconds, mills, and returns a human readable version.