 # How To Get The Number Of Minutes Between Two Dates In JavaScript ## Get The Number Of Minutes Between Two Dates In JavaScript

These are some methods that can help you get the number of minutes between two Dates in JavaScript when working with the Date object in JavaScript. You can learn more about the Date object in my previous tutorial.

### Calculate the duration indirectly

You can get the number of minutes between two Dates indirectly by calculating the duration between two Dates and converting it to minutes.

First, you calculate the durations between two dates and January 1, 1970 by the getTime() method.

Second, subtracting them. The result is the duration between two dates by milliseconds.

Finally, your remaining task is to convert the result from milliseconds to minutes. It will be the result of the calculation.

The getTime() method returns the number of milliseconds since January 1, 1970.

Syntax

getTime()

Parameters: None.

Return Value: The number of the milliseconds since January 1, 1970.

<!DOCTYPE html>
<html>
<title>
How To Get The Number Of Minutes Between Two Dates In JavaScript
</title>
<body style="text-align: left;">
<h1 style="color: rebeccapurple;">
How To Get The Number Of Minutes Between Two Dates In JavaScript
</h1>
<h2 style="color: black;">
Get The Number Of Minutes Between Two Dates In JavaScript
</h2>
<h3 style="color: black;">
Calculate the duration indirectly
</h3>
<script>
const firstDate = new Date("10/10/2022");
document.write("<br>The first date: " + firstDate);

const secondDate = new Date();
document.write("<br>The second date: " + secondDate);

// The function that gets the number of minutes between two Dates.
function getDuration(firstDate, secondDate) {
const seconds = (secondDate.getTime() - firstDate.getTime()) / 1000;
const minutes = Math.floor(seconds / 60);
return minutes
}

document.write("<br>The Number Of Minutes Between Two Dates: " + getDuration(firstDate, secondDate));
</script>
</body>
</html

Output

### Calculate the duration directly

You can get the number of minutes between two Dates directly by calculating the duration between two Dates and converting it to minutes.

First, you get the different days between two Dates by getDate() .

Second, you get the different times of two Dates and convert them to minutes by getHours() and getMinutes() methods.

Finally, you can get the result by adding the result of the step one to the result of the step 2.

<!DOCTYPE html>
<html>
<title>
How To Get The Number Of Minutes Between Two Dates In JavaScript
</title>
<body style="text-align: left;">
<h1 style="color: rebeccapurple;">
How To Get The Number Of Minutes Between Two Dates In JavaScript
</h1>
<h2 style="color: black;">
Get The Number Of Minutes Between Two Dates In JavaScript
</h2>
<h3 style="color: black;">
Calculate the duration directly
</h3>
<script>
const firstDate = new Date("10/10/2022");
document.write("<br>The first date: " + firstDate);

const secondDate = new Date();
document.write("<br>The second date: " + secondDate);

// The function that gets the number of minutes between two Dates.
function getDuration(firstDate, secondDate) {
const diffDate = secondDate.getDate() - firstDate.getDate();
const result = diffDate * 24 * 60 + (secondDate.getHours() * 60 + secondDate.getMinutes()) - (firstDate.getHours() * 60 + firstDate.getMinutes());
return result;
}

document.write("<br>The Number Of Minutes Between Two Dates: " + getDuration(firstDate, secondDate));
</script>
</body>
</html

Output

## Summary

You can calculate the duration of two Dates directly or indirectly and convert it to minutes to get the number of minutes between two Dates in JavaScript. Choose the way that is best for you. We hope this tutorial is helpful to you. Thanks!

Maybe you are interested: