If two dates are three days apart, no calendar date is equally distant from both. I'll show how I handle that gap in JavaScript with an explicit earlier-date rule, without confusing calendar-day counts with elapsed timestamps.
I built a small date-only calculator around that question. The useful implementation detail is not averaging two timestamps: it is making the meaning of “middle” explicit, normalizing the inputs, and keeping distance labels attached to the dates the user actually entered.
Normalize to Calendar Days Before Comparing
The inputs come from date pickers rather than timestamp fields. The calculation first checks that both dates exist, then converts them to Day.js values at the start of their respective days:
const d1 = dayjs ( date1 . value ) . startOf ( "day" ) ; const d2 = dayjs ( date2 . value ) . startOf ( "day" ) ;
const earlier = d1 . isBefore ( d2 ) ? d1 : d2 ; const later = d1 . isBefore ( d2 ) ? d2 : d1 ;
const totalDays = later .…