Convert Seconds To Milliseconds Javascript Code Example


Example 1: convert milliseconds to minutes and seconds javascript


const millisToMinutesAndSeconds = (millis) => {
var minutes = Math.floor(millis / 60000);
var seconds = ((millis % 60000) / 1000).toFixed(0);
//ES6 interpolated literals/template literals
//If seconds is less than 10 put a zero in front.
return `${minutes}:${(seconds < 10 ? "0" : "")}${seconds}`;
}

Example 2: convert milliseconds to seconds JS


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"

Example 3: what 1hr in milliseconds in javascript


/*
** Time in milliseconds

1 second = 1000 milliseconds
15 minutes = 900000 milliseconds
30 seconds = 30000 milliseconds
1 minute = 60000 milliseconds
5 minutes = 300000 milliseconds
30 minutes = 1800000 milliseconds
45 minutes = 2700000 milliseconds
1 hour = 3600000 milliseconds

*/

Example 4: convert milliseconds to time javascript


function msToTime(s) {

// Pad to 2 or 3 digits, default is 2
function pad(n, z) {
z = z || 2;
return ('00' + n).slice(-z);
}

var ms = s % 1000;
s = (s - ms) / 1000;
var secs = s % 60;
s = (s - secs) / 60;
var mins = s % 60;
var hrs = (s - mins) / 60;

return pad(hrs) + ':' + pad(mins) + ':' + pad(secs) + '.' + pad(ms, 3);
}

console.log(msToTime(55018))

Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable