How to calculate minutes between two dates in JavaScript ?
Given two dates and the task is to get the number of minutes between them using JavaScript.
Approach:
- Initialize both Date object.
- Subtract older date from the new date. It will give the number of milliseconds from 1 January, 1970.
- Convert milliseconds to minutes .
Example 1: This example uses the current date and next newYear date to get the difference of dates in minutes.
<!DOCTYPE HTML> <html> <head> <title> How to get the minutes between two dates in JavaScript ? </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 19px; font-weight: bold;"> </p> <button onClick = "GFG_Fun()"> click here </button> <p id = "GFG_DOWN" style = "color: green; font-size: 24px; font-weight: bold;"> </p> <!-- Script to calculate difference between two dates --> <script> var up = document.getElementById('GFG_UP'); var down = document.getElementById('GFG_DOWN'); // Declare dates var today = new Date(); var newYear = new Date("01-01-2020"); // Display the dates up.innerHTML = "Today's Date= "+ today.toLocaleDateString() + "<br> newYear's Date = " + newYear.toLocaleDateString(); // Function to calculate difference // between two dates function GFG_Fun() { var dif = (newYear - today); var dif = Math.round((dif/1000)/60); down.innerHTML = "Minutes left = " + dif; } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Example 2: This example uses the 2019 newYear and 2020 newYear date to get the difference in minutes.
<!DOCTYPE HTML> <html> <head> <title> How to get the minutes between two dates in JavaScript ? </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 19px; font-weight: bold;"> </p> <button onClick = "GFG_Fun()"> click here </button> <p id = "GFG_DOWN" style = "color: green; font-size: 24px; font-weight: bold;"> </p> <!-- Script to calculate difference between two dates --> <script> var up = document.getElementById('GFG_UP'); var down = document.getElementById('GFG_DOWN'); // Declare dates var newYear1 = new Date("01-01-2019"); var newYear2 = new Date("01-01-2020"); // Display the dates up.innerHTML = "First Date= "+ newYear1.toLocaleDateString() + "<br> Second Date = " + newYear2.toLocaleDateString(); // Function to calculate difference // between two dates function GFG_Fun() { var dif = (newYear2 - newYear1); var dif = Math.round((dif/1000)/60); down.innerHTML = "Minutes left = " + dif; } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- How to calculate the number of days between two dates in javascript?
- How to store all dates in an array present in between given two dates in JavaScript ?
- How to calculate the difference between two dates in PHP?
- JavaScript | Adding minutes to Date object
- Compare two dates using JavaScript
- How to select Min/Max dates in an array using JavaScript ?
- Return all dates between two dates in an array in PHP
- Calculate the width of the text in JavaScript
- How to calculate the yesterday's date in JavaScript ?
- How to calculate the date three months prior using JavaScript ?
- Comparing two dates in PHP
- Sort an array of dates in PHP
- Program to find the number of days between two dates in PHP
- Introduction to JavaScript Course | Learn how to Build a task tracker using JavaScript
- How to compare two JavaScript array objects using jQuery/JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

