Asked  7 Months ago    Answers:  5   Viewed   41 times

How can I convert seconds to an HH-MM-SS string using JavaScript?

 Answers

19

Updated (2020):

Please use @Frank's one line solution:

new Date(SECONDS * 1000).toISOString().substr(11, 8)

It is by far the best solution.


Old answer:

Use the Moment.js library.

Tuesday, June 1, 2021
 
williamcarswell
answered 7 Months ago
61

Use the MySQL SEC_TO_TIME function.

Example:

mysql> SELECT SEC_TO_TIME(2378);
        -> '00:39:38'

So you can write your SQL query as:

SELECT username, score, SEC_TO_TIME(timeran) as timetaken FROM gamescores ORDER BY Time DESC

Your code then becomes:

<?php
$result = mysql_query("SELECT username, score, SEC_TO_TIME(timeran) as timetaken FROM gamescores ORDER BY Time DESC");
if (mysql_num_rows($result)) {
for($rank=1; $row = mysql_fetch_assoc($result); $rank++) {
    echo "<tr>
    <td>{$rank}</td>
    <td>{$row['username']}</td>
    <td>{$row['timetaken']}</td>
    <td>{$row['score']}</td>
Wednesday, March 31, 2021
 
CMOS
answered 9 Months ago
36
String.prototype.toHHMMSS = function () {
    var sec_num = parseInt(this, 10); // don't forget the second param
    var hours   = Math.floor(sec_num / 3600);
    var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
    var seconds = sec_num - (hours * 3600) - (minutes * 60);

    if (hours   < 10) {hours   = "0"+hours;}
    if (minutes < 10) {minutes = "0"+minutes;}
    if (seconds < 10) {seconds = "0"+seconds;}
    return hours+':'+minutes+':'+seconds;
}

You can use it now like:

alert("5678".toHHMMSS());

Working snippet:

String.prototype.toHHMMSS = function () {
    var sec_num = parseInt(this, 10); // don't forget the second param
    var hours   = Math.floor(sec_num / 3600);
    var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
    var seconds = sec_num - (hours * 3600) - (minutes * 60);

    if (hours   < 10) {hours   = "0"+hours;}
    if (minutes < 10) {minutes = "0"+minutes;}
    if (seconds < 10) {seconds = "0"+seconds;}
    return hours + ':' + minutes + ':' + seconds;
}
    
console.log("5678".toHHMMSS());
Tuesday, June 1, 2021
 
Eddas
answered 7 Months ago
12

function secondsToHms(d) {
    d = Number(d);

    var h = Math.floor(d / 3600);
    var m = Math.floor(d % 3600 / 60);
    var s = Math.floor(d % 3600 % 60);

    return ('0' + h).slice(-2) + ":" + ('0' + m).slice(-2) + ":" + ('0' + s).slice(-2);
}

document.writeln('secondsToHms(10) = ' + secondsToHms(10) + '<br>');
document.writeln('secondsToHms(30) = ' + secondsToHms(30) + '<br>');
document.writeln('secondsToHms(60) = ' + secondsToHms(60) + '<br>');
document.writeln('secondsToHms(100) = ' + secondsToHms(100) + '<br>');
document.writeln('secondsToHms(119) = ' + secondsToHms(119) + '<br>');
document.writeln('secondsToHms(500) = ' + secondsToHms(500) + '<br>');
Tuesday, July 13, 2021
 
Jimenemex
answered 5 Months ago
10
echo date("U",strtotime('2012-01-18T11:45:00+01:00'));
Monday, August 9, 2021
 
Savageman
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share