- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- 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 Questions & Answers
- Python program to convert seconds into hours, minutes and seconds
- Converting seconds into days, hours, minutes and seconds in C++
- Convert HH:MM:SS to seconds with JavaScript?
- Hours and minutes from number of seconds using JavaScript
- Converting seconds in years days hours and minutes in JavaScript
- How to convert seconds to HH-MM-SS with JavaScript?
- C++ Program for converting hours into minutes and seconds
- How to get the seconds and minutes between two Instant timestamps in Java
- MySQL query to convert timediff() to seconds?
- MySQL DateTime Now()+5 days/hours/minutes/seconds?
- How to get hour, minutes and seconds in android using offset time API class?
- How to convert time seconds to h:m:s format in Python?
- How can I convert 'HH:MM:SS ' format to seconds in JavaScript
- Remove Seconds/ Milliseconds from Date and convert to ISO String?
- How to add 10 seconds to a JavaScript date object?