Convert timestamp to hh:mm:ss javascript
I think the most general (and cryptic) solution could be this Show
Or to copy & paste the shortest version
Some example outputs:
How it worksAlgorithm
It would also be nice to keep individual amounts in an array for easier formatting. For example given the input of 3785s the output should be Creating pipelineNaming the 3600 and 60 constants "breakpoints" you can write this algorithm into function as this
It returns an array where first item is the amount for given breakpoint and the rest of the array is given by the callback. Reusing the Then you also need the "final" callback that ends this pipeline. In another words you used all breakpoints and now you have only the remainder. Since you have already the answer at 3) you should use some sort of identity function, in this case You can now write the pipeline like this
Cool right? Generalizing using for-loopNow you can generalize with a variable amount of breakpoints and create a for-loop that will compose individial
Using Array.prototype.reduce()Now you can rewrite this for-loop into reducer for shorter and more functional code. In other words rewrite function composition into the reducer.
The
accumulator You can now inline the function
Which is the final form. Then you just appy mapping to string with padded 0's on left and join the strings with More generalizationsWrapping the reducer into function
you now have very general algorithm you can work with. For example: Convert easily (the weird) us length standardsGiven the standards
123456 in = 1 mi, 1669 yd, 1 feet and 0 in Or you can somewhat convert to decimal or binary representations
Works also with floating point breakpointsSince Javascript supports
The edge case of no breakpoints is also naturally covered
How to convert time to hh mm ss in javascript?To convert seconds to HH:MM:SS :
Multiply the seconds by 1000 to get milliseconds. Pass the milliseconds to the Date() constructor. Use the toISOString() method on the Date object. Get the hh:mm:ss portion of the string.
How to convert seconds to time in js?javascript convert seconds to minutes seconds. function convertHMS(value) {. const sec = parseInt(value, 10); // convert value to number if it's string.. let hours = Math. ... . let minutes = Math. ... . let seconds = sec - (hours * 3600) - (minutes * 60); // get seconds.. // add 0 if value < 10; Example: 2 => 02.. How do you convert seconds to HH mm SS in SQL?SELECT CONVERT(varchar, DATEADD(ms, 121.25 * 1000, 0), 114) is it. ... . This won't work if you have more than 24 hours worth of seconds. ... . Very neat. ... . If you have SQL 2008+ and just want the time portion; instead of converting to varchar: SELECT CONVERT(time, DATEADD(ms, 121.25 * 1000, 0)) ... . You can also do FORMAT(..., 'mm:ss'). How do you convert seconds to HH mm SS format in Excel?As with Excel, the first step to converting elapsed second to time is to divide the value by 86400. To format the cells for mm:ss, select Format > Number > More Formats > More date and time formats from the Menu.
|