Search code examples
javascripttimedatetime-formatstring-parsing

How to parse a string containing number of seconds to h/m/s format


I have a string that contains a number of seconds outputted by a timer (please note that the string is updated every second):

5162

How can I parse this string to hour/minute/second format in javascript?


Solution

  • You don't need to parse-just do some math.

    Pseudocode:

    sec=5162
    hours = truncate(sec / 3600)
    sec= sec modulo 3600
    mins = truncate (sec/60)
    sec = sec modulo 60
    

    You don't mention what language you are using; in c, you can use sprintf to put all the pieces together into a string.