Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mid-point between two dates

What is the most efficient and syntactically elegant way to determine the half way point between two dates in Javascript.

var difference = date2.getTime() - date1.getTime();
var midpoint = new Date(date1.getTime() + difference / 2);

Is this a good approach?

like image 889
Dom Vinyard Avatar asked Dec 10 '13 11:12

Dom Vinyard


Video Answer


1 Answers

That is indeed the most efficient way, with one correction:

var midpoint = new Date((date1.getTime() + date2.getTime()) / 2);

The mid-point is the average of the two points.

like image 63
Tibos Avatar answered Nov 15 '22 22:11

Tibos