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 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))
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
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 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());
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.
How to change this countdown timer into minutes and seconds?
One minute = total / 60.
Seconds = total % 60.
Add 0 if minutes or seconds below 10.
Just add this calculations before adding to html.
var test = 3600;
$('button').click(function(){
if (localStorage.getItem("counter")) {
if (localStorage.getItem("counter") <= 0) {
var value = test;
}
else {
var value = localStorage.getItem("counter");
}
}
else {
var value = test;
}
$('#divCounter').text(value);
var counter = function() {
if (value == 0) {
localStorage.setItem("counter", test);
value = 0;
}
else {
value = parseInt(value) - 1;
localStorage.setItem("counter", value);
}
var minutes = Math.floor(value / 60)
var seconds = value % 60
if (minutes<10) minutes = '0'+minutes
if (seconds<10) seconds = '0'+seconds
$('#divCounter').text(minutes+':'+seconds);
};
var interval = setInterval(function() { counter(); }, 1000);
});
Converting milliseconds to minutes and seconds with Javascript
function millisToMinutesAndSeconds(millis) {
var minutes = Math.floor(millis / 60000);
var seconds = ((millis % 60000) / 1000).toFixed(0);
return minutes + ":" + (seconds < 10 ? '0' : '') + seconds;
}
millisToMinutesAndSeconds(298999); // "4:59"
millisToMinutesAndSeconds(60999); // "1:01"
As User HelpingHand pointed in the comments the return statement should be:
return (
seconds == 60 ?
(minutes+1) + ":00" :
minutes + ":" + (seconds < 10 ? "0" : "") + seconds
);
Related Topics
Chrome Extension - How to Get Http Response Body
How to Inherit from a Class in JavaScript
Copy/Put Text on the Clipboard with Firefox, Safari and Chrome
Create Array of Unique Objects by Property
React Router with Optional Path Parameter
Resize a Div on Border Drag and Drop Without Adding Extra Markup
Change Image in HTML Page Every Few Seconds
How to Tell Google Translate to Not Translate a Section of a Website
What Is "Context" in Jquery Selector
Write/Add Data in JSON File Using Node.Js
JavaScript Validation for Empty Input Field
Rails 4 Turbo-Link Prevents Jquery Scripts from Working
How to Display Pie Chart Data Values of Each Slice in Chart.Js