Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to calculate minutes between two dates in JavaScript?
In this article, you will understand how to calculate minutes between two dates in JavaScript.
The Date object works with dates and times. Date objects are created with new Date(). To calculate minutes between dates, we convert the time difference from milliseconds to minutes using mathematical operations.
Method 1: Using a Function
In this example, we create a reusable function to find the time difference in minutes.
function minutesDiff(dateTimeValue2, dateTimeValue1) {
var differenceValue = (dateTimeValue2.getTime() - dateTimeValue1.getTime()) / 1000;
differenceValue /= 60;
return Math.abs(Math.round(differenceValue));
}
dateTimeValue1 = new Date(2020, 11, 12);
console.log("The first date time value is defined as: ", dateTimeValue1);
dateTimeValue2 = new Date(2020, 11, 13);
console.log("The second date time value is defined as: ", dateTimeValue2);
console.log("\nThe difference in the two date time values in minutes is: ");
console.log(minutesDiff(dateTimeValue1, dateTimeValue2));
The first date time value is defined as: 2020-12-12T00:00:00.000Z The second date time value is defined as: 2020-12-13T00:00:00.000Z The difference in the two date time values in minutes is: 1440
How It Works
Step 1 ? Define two date time values dateTimeValue1 and dateTimeValue2.
Step 2 ? Define a function minutesDiff that takes two date values as parameters.
Step 3 ? In the function, calculate the time difference by subtracting the date values and dividing it by 1000 (converts milliseconds to seconds). Divide the result again by 60 to get the minutes.
Step 4 ? Use Math.abs() to get absolute value and Math.round() to round to nearest integer.
Method 2: Direct Calculation
In this example, we compute the time difference without functions for simpler cases.
dateTimeValue1 = new Date(2020, 11, 12);
console.log("The first date time value is defined as: ", dateTimeValue1);
dateTimeValue2 = new Date(2020, 11, 13);
console.log("The second date time value is defined as: ", dateTimeValue2);
console.log("\nThe difference in the two date time values in minutes is: ");
var differenceValue = (dateTimeValue2.getTime() - dateTimeValue1.getTime()) / 1000;
differenceValue /= 60;
let result = Math.abs(Math.round(differenceValue));
console.log(result);
The first date time value is defined as: 2020-12-12T00:00:00.000Z The second date time value is defined as: 2020-12-13T00:00:00.000Z The difference in the two date time values in minutes is: 1440
Working with Time Components
Here's an example calculating minutes between specific times on the same day:
// Same day, different times
let startTime = new Date(2024, 0, 15, 10, 30, 0); // 10:30 AM
let endTime = new Date(2024, 0, 15, 14, 45, 0); // 2:45 PM
let minutesDifference = (endTime.getTime() - startTime.getTime()) / (1000 * 60);
console.log("Start time:", startTime.toLocaleTimeString());
console.log("End time:", endTime.toLocaleTimeString());
console.log("Minutes difference:", minutesDifference);
Start time: 10:30:00 AM End time: 2:45:00 PM Minutes difference: 255
Key Points
getTime()returns milliseconds since January 1, 1970 UTCDivide by 1000 to convert milliseconds to seconds
Divide by 60 to convert seconds to minutes
Use
Math.abs()to ensure positive result regardless of date orderUse
Math.round()to get whole minutes
Conclusion
Calculating minutes between dates involves getting the time difference in milliseconds using getTime() and converting to minutes by dividing by 60,000. Use functions for reusable code or direct calculation for simple cases.
