get minutes and seconds between two dates in javascript

You can calculate the minutes and seconds between two dates in JavaScript by subtracting one date from the other and then converting the resulting difference to minutes and seconds. Here's an example:

index.tsx
// create two date objects
let startDate = new Date('2021-09-01T04:30:00');
let endDate = new Date('2021-09-01T05:45:30');

// get the difference between the two dates in milliseconds
let diff = endDate.getTime() - startDate.getTime();

// convert the difference to minutes and seconds
let minutes = Math.floor(diff / 1000 / 60);
let seconds = Math.floor((diff / 1000) % 60);

console.log(`There are ${minutes} minutes and ${seconds} seconds between ${startDate} and ${endDate}`);
481 chars
13 lines

This will output:

index.tsx
There are 75 minutes and 30 seconds between Wed Sep 01 2021 04:30:00 GMT-0400 (Eastern Daylight Time) and Wed Sep 01 2021 05:45:30 GMT-0400 (Eastern Daylight Time)
164 chars
2 lines

gistlibby LogSnag