Open In App

How to calculate minutes between two dates in JavaScript ?

Improve
Improve
Like Article
Like
Save
Share
Report

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




// Declare dates
let today = new Date();
let newYear = new Date("01-01-2024");
 
// Display the dates
console.log("Today's Date: " + today.toLocaleDateString()
    + "\nNew Year's Date: " + newYear.toLocaleDateString());
 
// Calculate difference between two dates in minutes
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




// Declare dates
let newYear1 = new Date("01-01-2019");
let newYear2 = new Date("01-01-2020");
 
 
// Display the dates
console.log("First Date: " + newYear1.toLocaleDateString()
    + "\Second Date: " + newYear2.toLocaleDateString());
 
// Calculate difference between two dates in minutes
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

Last Updated : 12 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads