How to Convert Seconds to Minutes and Hours in JavaScript

How to convert seconds to minutes and hours in javascript

I think you would find this solution very helpful.

You modify the display format to fit your needs with something like this -

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);

var hDisplay = h > 0 ? h + (h == 1 ? " hour, " : " hours, ") : "";
var mDisplay = m > 0 ? m + (m == 1 ? " minute, " : " minutes, ") : "";
var sDisplay = s > 0 ? s + (s == 1 ? " second" : " seconds") : "";
return hDisplay + mDisplay + sDisplay;
}

Javascript seconds to minutes and seconds

To get the number of full minutes, divide the number of total seconds by 60 (60 seconds/minute):

var minutes = Math.floor(time / 60);

And to get the remaining seconds, multiply the full minutes with 60 and subtract from the total seconds:

var seconds = time - minutes * 60;

Now if you also want to get the full hours too, divide the number of total seconds by 3600 (60 minutes/hour · 60 seconds/minute) first, then calculate the remaining seconds:

var hours = Math.floor(time / 3600);
time = time - hours * 3600;

Then you calculate the full minutes and remaining seconds.

Bonus:

Use the following code to pretty-print the time (suggested by Dru)

function str_pad_left(string,pad,length) {
return (new Array(length+1).join(pad)+string).slice(-length);
}

var finalTime = str_pad_left(minutes,'0',2)+':'+str_pad_left(seconds,'0',2);

Convert seconds to HH-MM-SS with JavaScript?

Updated (2020):

Please use @Frank's one line solution:

new Date(SECONDS * 1000).toISOString().substring(11, 16)

If SECONDS<3600 and if you want to show only MM:SS then use below code:

new Date(SECONDS * 1000).toISOString().substring(14, 19)

It is by far the best solution.


Old answer:

Use the Moment.js library.

JavaScript seconds to time string with format hh:mm:ss

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());

how to convert seconds to minutes using javascript...?

Try something like this:

function convert(value) {
return Math.floor(value / 60) + ":" + (value % 60 ? value % 60 : '00')
}

DEMO

Converting seconds into Hours, Minutes, and Seconds in JavaScript

Subracting the two dates will give you the difference in ms.
For calculating the difference in hours you should use:

 const timeLeft = (expirationTime - now) / 1000 / 3600;

Note that if you're planning to do more sophisticated date arithmetic I recommend using a library like momentjs.

How to convert Seconds to Time in Javascript

Just test

const convert = secs => { 
const sign = secs < 0
const hhmmss = new Date(Math.abs(secs) * 1000).toISOString().substr(11, 8);
return sign ? '-' + hhmmss : hhmmss
}


console.log(convert(540));
console.log(convert(-2820));

Convert seconds to minutes and seconds in MM:SS format

You could use padStart for min

ret += "" + String(mins).padStart(2, '0') + ":" + (secs < 10 ? "0" : "");

function format(time) {
// Hours, minutes and seconds
var hrs = ~~(time / 3600);
var mins = ~~((time % 3600) / 60);
var secs = ~~time % 60;

// Output like "1:01" or "4:03:59" or "123:03:59"
var ret = "";
if (hrs > 0) {
ret += "" + hrs + ":" + (mins < 10 ? "0" : "");
}
ret += "" + String(mins).padStart(2, '0') + ":" + (secs < 10 ? "0" : "");
ret += "" + secs;
return ret;
}

console.log(format(150))

(React) How should i convert seconds passed as props do hours minutes seconds

minutes = this.props.seconds/60;
hours = this.props.seconds/3600;

Have you tried this?



Related Topics



Leave a reply



Submit