how.wtf

Calculate the middle between two dates in JavaScript

· Thomas Taylor

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

Determine the middle between two dates

1const startDate = new Date('Mar 10 2023');
2const endDate = new Date('Mar 15 2023');
3
4const midDate = new Date((startDate.getTime() + endDate.getTime()) / 2);
5console.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.

#javascript  

Reply to this post by email ↪