javascript get minutes between dates in javascript

To get the difference in minutes between two dates in JavaScript, we can create two Date objects, subtract one from the other, and then divide the result by the number of milliseconds in a minute (60,000).

Here's an example:

index.tsx
const date1 = new Date("2022-05-01T12:00:00");
const date2 = new Date("2022-05-01T12:30:00");

const diffInMilliseconds = date2 - date1;
const diffInMinutes = Math.floor(diffInMilliseconds / 60000);

console.log(diffInMinutes); // Output: 30
242 chars
8 lines

In the above code snippet, we create two Date objects for May 1st, 2022, at 12:00 PM and 12:30 PM respectively. We then subtract the first date (date1) from the second date (date2) to get the difference between them in milliseconds. We convert this difference to minutes by dividing by 60,000 (60 seconds * 1000 milliseconds), and then round down the result using Math.floor() to get a whole number of minutes. Finally, we log the result to the console.

gistlibby LogSnag