To calculate the difference in minutes between two dates in JavaScript, you can use the getTime()
method to get the timestamps of the two dates, and then subtract them to get the difference in milliseconds. Finally, divide the difference by 60000 (number of milliseconds in a minute) to get the difference in minutes. Here's an example:
// Two date objects
const date1 = new Date('2022-01-01 12:00:00');
const date2 = new Date('2022-01-01 12:10:30');
// Calculate the difference in minutes
const differenceInMilliseconds = date2.getTime() - date1.getTime();
const differenceInMinutes = Math.round(differenceInMilliseconds / 60000);
console.log(differenceInMinutes); // Output: 10
In the example above, differenceInMinutes
will store the difference in minutes between date1
and date2
, which is 10 in this case.