Calculate the middle between two dates in JavaScript

In TypeScript or JavaScript, determining the midpoint between two dates is easy using the native Date library.

Determine the middle between two dates

1
2
3
4
5
const startDate = new Date('Mar 10 2023');
const endDate = new Date('Mar 15 2023');

const midDate = new Date((startDate.getTime() + endDate.getTime()) / 2);
console.log(midDate) // 2023-03-12T12:00:00.000Z

In the example above, getTime() returns the number of seconds since epoch. Adding the seconds and dividing by two gives the midpoint.