How to calculate minutes between two dates in JavaScript ?
Last Updated :
12 Jun, 2023
Given two dates and the task is to get the number of minutes between them using JavaScript.
Approach:
- Initialize both Date object.
- Subtract the 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 the next new year date to get the difference of dates in minutes.
Javascript
let today = new Date();
let newYear = new Date( "01-01-2024" );
console.log( "Today's Date: " + today.toLocaleDateString()
+ "\nNew Year's Date: " + newYear.toLocaleDateString());
let dif = (newYear - today);
dif = Math.round((dif / 1000) / 60);
console.log( "Minutes left: " + dif);
|
Output
Today's Date: 6/12/2023
New Year's Date: 1/1/2024
Minutes left: 292151
Example 2: This example uses the 2019 newYear and 2020 newYear date to get the difference in minutes.
Javascript
let newYear1 = new Date( "01-01-2019" );
let newYear2 = new Date( "01-01-2020" );
console.log( "First Date: " + newYear1.toLocaleDateString()
+ "\Second Date: " + newYear2.toLocaleDateString());
let dif = (newYear2 - newYear1);
dif = Math.round((dif / 1000) / 60);
console.log( "Minutes left: " + dif);
|
Output
First Date: 1/1/2019Second Date: 1/1/2020
Minutes left: 525600
Share your thoughts in the comments
Please Login to comment...