I need to calculate the days between two dates with 6 decimals. I'm using the following function:
/**
* Calculates the day difference for two given dates.
*
* @param {Date} from , the start date
* @param {Date} to , the end date
*
* @return {Number} the day difference
*/
function calculateDayDifference( from, to ) {
var dayDifference;
const ONEDAY = 1000 * 60 * 60 * 24;
if ( from != null && to != null ) {
dayDifference = Math.abs( from - to ) / ONEDAY;
}
return dayDifference;
}
The problem is, that the calculation is not correct for the following example:
- from 23.10.2013 10:00 to 01.11.2013 00:00
it returns 8.625, but the correct value is 8,583333. This wrong value differs 1 hour from the correct value.
In the following case:
- from 01.11.2013 00:00 to 07.11.2013 10:00
the returned value 6,416667 is correct.