This article can help you learn how to get the number of minutes between two dates in JavaScript when working with the Date object in JavaScript. Follow this article to learn more about it with the explanation and examples below.
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.
Look at the example below to learn more about this method.
<!DOCTYPE html> <html> <head> <title> How To Get The Number Of Minutes Between Two Dates In JavaScript </title> </head> <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.
Look at the example below to learn more about this method.
<!DOCTYPE html> <html> <head> <title> How To Get The Number Of Minutes Between Two Dates In JavaScript </title> </head> <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:
- Subtract Months from a Date in JavaScript
- Get ISO Date without the Milliseconds using JavaScript
- Get a GMT timestamp using JavaScript

My name is Thomas Valen. As a software developer, I am well-versed in programming languages. Don’t worry if you’re having trouble with the C, C++, Java, Python, JavaScript, or R programming languages. I’m here to assist you!
Name of the university: PTIT
Major: IT
Programming Languages: C, C++, Java, Python, JavaScript, R