How to convert JavaScript seconds to minutes and seconds?

Front End TechnologyJavascriptWeb Development

In this tutorial, we will learn to convert JavaScript seconds to minutes and seconds. The problem is that we have given the total number of seconds, and we need to represent it in the minutes and second format.

We can perform some basic Mathematics operations and solve our problems. Here, we have two different ways to convert the seconds to minutes and seconds.

Using the Math.floor() Method

In this approach, we will use the Math.floor() method. We will divide the total number of seconds by 60 to convert it into the minutes and apply the Math.floor() method to round down the float minutes. After that, we will take the modulo of seconds by 60 to get the extra remaining seconds.

Syntax

Users can follow the syntax below to convert seconds to minutes and seconds.

let minutes = Math.floor(seconds / 60);
let extraSeconds = seconds % 60;
minutes = minutes < 10 ? "0" + minutes : minutes;
extraSeconds = extraSeconds < 10 ? "0" + extraSeconds : extraSeconds;

Algorithm

  • Step 1 − Divide the total seconds by 60 to convert it to minutes.

  • Step 2 − Apply the Math.floor() method to minutes to round down it.

  • Step 3 − Take modulo of total seconds by 60 to get the remaining seconds.

  • Step 4 − If minutes or seconds are less than 10, append 0 before them.

Example

In the example below, we have created the convertStoMs() function to convert the seconds to minutes and the second format using the above algorithm. We have invoked the function for the different values of seconds, and users can observe the result in the output.

<html> <head> </head> <body> <h2>Convert seconds to minutes and seconds in JavaScript.</h2> <h4>Using the <i>Math.floor()</i> method to convert the different values of seconds to minutes and seconds.</h4> <p id = "output"></p> <script> let output = document.getElementById("output"); function convertStoMs(seconds) { let minutes = Math.floor(seconds / 60); let extraSeconds = seconds % 60; minutes = minutes < 10 ? "0" + minutes : minutes; extraSeconds = extraSeconds< 10 ? "0" + extraSeconds : extraSeconds; output.innerHTML += seconds + " == " + minutes + " : " + extraSeconds + "<br/>"; } convertStoMs(159); convertStoMs(234567); convertStoMs(9); </script> </body> </html>

Using the Bitwise Double Not (~~) Operator

In this approach, we will use the Double Not (~~) operator to round down the minutes instead of the Math.floor() method. The Double Not operator is the replacement for the Math.floor() method.

Users can follow the syntax below to use the Double Not operator.

Syntax

let minutes = ~~(seconds / 60);
let extraSeconds = seconds % 60;

Example

In the example below, we will convert the seconds to minutes by dividing the seconds by 60 and rounding down it using the Double Not (~~) operator. To get the remaining seconds, we will perform a modulo operation of total seconds by 60.

<html> <head> </head> <body> <h2>Convert seconds to minutes and seconds in JavaScript.</h2> <h4>Using the <i>Double Not (~~)</i> method to convert the different values of seconds to minutes and seconds.</h4> <p id = "output"></p> <script> let output = document.getElementById("output"); function convertStoMs(seconds) { let minutes = ~~(seconds / 60); let extraSeconds = seconds % 60; output.innerHTML += seconds + " == " + minutes + " : " + extraSeconds + "<br/>"; } convertStoMs(421); convertStoMs(2876); convertStoMs(10); </script> </body> </html>

We have learned the two approaches to converting the total number of seconds into minutes and seconds. Users can use the Bitwise Double Not (~~) operator to make the code faster, as the Math.floor() method is much slower than the Bitwise operators.

raja
Updated on 17-Aug-2022 12:35:26

Advertisements