Convert seconds into hours, minutes, seconds

Solved
chabinot Posted messages 391 Status Member -  
 lessIsMore -
```html Hello,
In JavaScript, I have a duration of 10048.608 seconds.
I would like to convert it to hours, minutes, and seconds,
the result should be 2:47:28.
I manage to get 2 hours and 47 minutes. I'm stuck on the seconds.
Here is an excerpt from my code:
 length = audio.duration; // 10048.608 minutes = length / 60; // 167.4768 hours = minutes / 60; // 2 hours minutes = minutes % 60; // 47 minutes 

Thank you for your help.
Best regards. ```

3 answers

  1. 3IR3 Posted messages 936 Registration date   Status Member Last intervention   232
     
    Hi,

    Personally, I wouldn't have broken it down the same way.

    length = audio.duration; // 10048.608
    hours = length / 3600; // 2 hours remaining 0.79128 hour
    minutes = 0.79128 * 60 // 47 minutes remaining 0.4768 minute
    seconds = 0.4768 * 60 // 28 seconds remaining 0.48 seconds
    1
  2. jordane45 Posted messages 30426 Registration date   Status Moderator Last intervention   4 830
     
    Hello,
     function secToTime(totalSeconds){ hours = Math.floor(totalSeconds / 3600); totalSeconds %= 3600; minutes = Math.floor(totalSeconds / 60); seconds = Math.floor(totalSeconds % 60); return hours + ":" + minutes + ":" + seconds ; } var duree = 10048.608; var result = secToTime(duree); alert(result); 


    --
    Best regards,
    Jordane
    0
  3. lessIsMore
     
    Hello,
    with the Date object and the Unix Timestamp, we can skip calculations and a lot of writing:

    let duration = new Date(); duration.setTime(10048608);//-- number in seconds X 1000 for milliseconds alert(duration.getHours()-1+'h'+duration.getMinutes()+'min'+duration.getSeconds()); 


    The -1 removes the 0 hour from the UNIX date (at least I think so).
    0