小数点以下 6 桁で 2 つの日付間の日数を計算する必要があります。私は次の機能を使用しています:
/**
* 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;
}
問題は、次の例では計算が正しくないことです。
- 2013 年 10 月 23 日 10:00から2013 年 11 月 1 日 00:00まで
8.625を返しますが、正しい値は8,583333です。この誤った値は、正しい値と 1 時間異なります。
次の場合:
- 01.11.2013 00:00から07.11.2013 10:00まで
戻り値6,416667は正しいです。