Convert seconds into hours, minutes, seconds
Solved
chabinot
Posted messages
391
Status
Membre
-
lessIsMore -
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:
Thank you for your help.
Best regards. ```
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 réponses
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
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
Hello,
--
Best regards,
Jordane
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
Hello,
with the Date object and the Unix Timestamp, we can skip calculations and a lot of writing:
The -1 removes the 0 hour from the UNIX date (at least I think so).
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).