get duration between start and end time in minutes in javascript

You can get the duration between the start and end time in minutes by calculating the difference between two dates in milliseconds and then converting it into minutes.

Here's an example:

index.tsx
const startTime = new Date('2021-07-01T10:00:00');
const endTime = new Date('2021-07-01T10:30:00');

const duration = (endTime.getTime() - startTime.getTime()) / (1000 * 60);

console.log(duration); // Output: 30
213 chars
7 lines

In the example above, we first create two Date objects representing the start and end time. We then calculate the duration between the two times by subtracting the start time from the end time, and dividing the result by the number of milliseconds in a minute. Finally, we log the duration in minutes to the console.

Note that the getTime() method returns the number of milliseconds since January 1, 1970, 00:00:00 UTC, so we divide the result by the number of milliseconds in a minute (1000 * 60) to get the duration in minutes.

related categories

gistlibby LogSnag