How To Convert Milliseconds To Minutes And Seconds In JavaScript

How To Convert Milliseconds to Minutes and Seconds in JavaScript

To convert milliseconds to minutes and seconds in JavaScript, we can use two methods: Creating a function with Math.floor() and toFixed() method, or use the getMinutes() and getSeconds() method. Check out the details below to get more information.

Convert milliseconds to minutes and seconds in JavaScript

Create the function with Math.floor() and toFixed() method

To convert miliseconds to minutes and seconds in Javascript, we can easily create a function to do it by using the Math.floor() method and toFixed() method. 

The toFixed() method

Syntax:

toFixed(x)

Parameter: 

  • x: Number of decimals. Default is 0.

Return value: The representation of a number with (Or without) decimals.

The Math.floor() method

Syntax:

Math.floor(x)

Parameter: 

  • x: A number. 

Return value: The nearest integer to the number. Rounding DOWN.

Example: 

In this example, we will create a toMinutesAndSeconds function that gets the number of minutes by rounding millis divided by 60000 via the Math.floor() method. Get seconds by using millis mod 60000, then, divide 1000 and rounding by using toFixed().

function toMinutesAndSeconds(millis) {
    // Math.floor() method to rounding down to minutes
    var minutes = Math.floor(millis / 60000);

    // toFixed() method to rounding to seconds
    var seconds = ((millis % 60000) / 1000).toFixed(0);
  
    return minutes + ":" + (seconds < 10 ? '0' : '') + seconds;
}

console.log("After converting 666000 miliseconds to minutes and seconds: " + toMinutesAndSeconds(666000))  // "11:06"
console.log("After converting 298999 miliseconds to minutes and seconds: " + toMinutesAndSeconds(298999)) // "4:59"

Output

After converting 666000 miliseconds to minutes and seconds: 
11:06
After converting 298999 miliseconds to minutes and seconds: 
4:59

Use the getMinutes() and getSeconds() method

The other simple way to convert miliseconds to minutes and seconds is use the getMinutes() method and getSeconds() method.

The getMinutes() method

Syntax:

getMinutes()

Parameter: None.

Return value: The minutes of the date.

The getSeconds() method

Syntax:

getSeconds()

Parameter: None.

Return value: The seconds of the date.

Example:

const date1 = new Date(298999); 
minutes = date1.getMinutes();
seconds = date1.getSeconds();
console.log("After converting 298999 miliseconds to minutes and seconds: " + minutes + ":" + seconds);
 
const date2 = new Date(666000); 
minutes = date2.getMinutes();
seconds = date2.getSeconds();
console.log("After converting 666000 miliseconds to minutes and seconds: " + minutes + ":" + seconds);

Output

After converting 298999 milliseconds to minutes and seconds: 
4:58
After converting 666000 milliseconds to minutes and seconds: 
11:6

Summary

In this tutorial, we have explained how to convert milliseconds to minutes and seconds in JavaScript. We always hope this tutorial is helpful to you. Leave your comment here if you have any questions or comments to improve the article. Thanks for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *