- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# How to convert JavaScript seconds to minutes and seconds?

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.

- Related Articles
- Python program to convert seconds into hours, minutes and seconds
- Golang program to convert milliseconds to minutes and seconds
- Convert 40 minutes into seconds.
- How to Convert Hours, Minutes and Seconds to Decimal Hours in Excel?
- Convert 7290 seconds into hours and minutes.
- How to Convert Decimal Degrees to Degrees Minutes Seconds in Excel
- How to Convert Time to Decimal Hours /Minutes/ Seconds in Excel?
- Convert HH:MM:SS to seconds with JavaScript?
- How to convert seconds to HH-MM-SS with JavaScript?
- Converting seconds into days, hours, minutes and seconds in C++
- Hours and minutes from number of seconds using JavaScript
- How to Convert Time Difference between Two Times to Number (Hours/Minutes/Seconds) in Excel?
- Swift integer conversion to Hours/Minutes/Seconds
- Converting seconds in years days hours and minutes in JavaScript
- Add the following:2 hours 13 minutes 40 seconds and 4 hours 23 minutes 35 seconds.