
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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(). JavaScript will use the browser's time zone and display a date as a full text string.
Example 1
In this example, we use a function to find the time difference.
function minutesDiff(dateTimeValue2, dateTimeValue1) { var differenceValue =(dateTimeValue2.getTime() - dateTimeValue1.getTime()) / 1000; differenceValue /= 60; return Math.abs(Math.round(differenceValue)); } dateTimeValue1 = new Date(2020,12,12); console.log("The first date time value is defined as: ", dateTimeValue1) dateTimeValue2 = new Date(2020,12,13); console.log("The second date time value is defined as: ", dateTimeValue2) console.log("
The difference in the two date time values in minutes is: ") console.log(minutesDiff(dateTimeValue1, dateTimeValue2));
Explanation
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. Divide the result again by 60 to get the minutes.
Step 4 − Display the minutes difference as the result.
Example 2
In this example, we compute the time difference without functions.
dateTimeValue1 = new Date(2020,12,12); console.log("The first date time value is defined as: ", dateTimeValue1) dateTimeValue2 = new Date(2020,12,13); console.log("The second date time value is defined as: ", dateTimeValue2) console.log("
The 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)
Explanation
Step 1 −Define two date time values dateTimeValue1 and dateTimeValue2.
Step 2 −Calculate the time difference by subtracting the date values and dividing it by 1000. Divide the result again by 60 to get the minutes.
Step 3 −Display the minutes difference as the result.
- Related Articles
- Calculate minutes between two dates in C#
- How to calculate the difference between two dates in JavaScript?
- How to calculate average between two dates in Excel?
- How to calculate the midpoint between two dates in Excel?
- How to calculate the percentage between two dates in Excel?
- How to calculate time difference between two times or dates?
- How to calculate the weeks and days between two dates in Excel?
- How to store all dates in an array present in between given two dates in JavaScript?
- How to list all dates between two dates in Excel?
- How to get the number of seconds between two Dates in JavaScript?
- How to check if one date is between two dates in JavaScript?
- How to get the number of days between two Dates in JavaScript?
- How can I calculate full 24hour days between two specified dates in MySQL?
- How do I calculate number of days between two dates using Python?
- How to query between two dates in MySQL?
