Calculate middle date

date, javascript

Solution

"Middle of two dates" is not unambiguously defined - you must decide how to handle dates an odd number of days apart (e.g. what is the middle date between 1st and 4th of a month, or between 1st and 2nd), and what to do with the time portion of the date object.

The concrete problem with your approach is that dates are not numbers, so you cannot add them and divide them by two. To do that, use the `getTime()` method to obtain the number of seconds since the epoch, and operate on that:

var middate = new Date((startdate.getTime() + enddate.getTime()) / 2);

This will give you the middle between two dates, treating them as points in time.

Problem

I want to calculate the middle between two dates in Javascript. So I tried: ``` var middate = (startdate+enddate)/2; console.log(middate); ``` which logs ``` NaN ``` What is the problem?

Original source