3

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.

Community
  • 1
  • 1
BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96
  • 2
    You seem to have a problem with daylight saving times. One hour was "added" on 2013-10-27 03:00 CEST (my timezone) when we got back to CET. I'm afraid Javascript has no provision for dealing with DST out of the box. – Frédéric Hamidi Oct 28 '13 at 09:25
  • [See](http://stackoverflow.com/questions/19631025/calculate-days-between-dates-with-decimals/19631166#comment29147584_19631168) – BuZZ-dEE Oct 28 '13 at 10:46

4 Answers4

2

This is due to daylight saving.

The code is working perfectly, 8.625 is the right amount of time between those two date. If you want not to deal with the time changes, use UTC time, it does not have daylight saving and is available in (almost?) all languages.

Salketer
  • 14,263
  • 2
  • 30
  • 58
  • Thanks, your hint was useful, but if I use UTC, I get the value **8,666667** instead of [**8,583333**](http://stackoverflow.com/a/19632549/183704). – BuZZ-dEE Oct 28 '13 at 10:46
  • 1
    Are you sure you are comparing UTC with UTC? That is an exact 2 hour difference, which would mean you are comparing a UTC-2 timezone to a neutral UTC. – Salketer Oct 28 '13 at 12:44
  • I used: `var fromUTC = new Date( from.getUTCFullYear(), from.getUTCMonth(), from.getUTCDate(), from.getUTCHours(), from.getUTCMinutes(), from.getUTCSeconds() ), toUTC = new Date( to.getUTCFullYear(), to.getUTCMonth(), to.getUTCDate(), to.getUTCHours(), to.getUTCMinutes(), to.getUTCSeconds() );` and then calculate `dayDifference = Math.abs( fromUTC - toUTC ) / ONEDAY;`. – BuZZ-dEE Oct 28 '13 at 14:57
0

Use .toFixed(6)

function calculateDayDifference( from, to ) {
    var dayDifference;
    const ONEDAY = 1000 * 60 * 60 * 24;

    if ( from != null && to != null ) {     
        dayDifference = (Math.abs( from - to )).toFixed(6); / ONEDAY;
    }
    return dayDifference;
}
Igl3
  • 4,900
  • 5
  • 35
  • 69
0

In javascript Math.abs function may be give Rounded precision point after in float value.

I have tested one of the example you can check it out with this .toFixed(6), You may get solution.

today=new Date()
var christmas=new Date(today.getFullYear(), 11, 25) 
if (today.getMonth()==11 && today.getDate()>25) 
christmas.setFullYear(christmas.getFullYear()+1) 
var one_day=1000*60*60*24

document.write(((christmas.getTime()-today.getTime())/(one_day)).toFixed(6)+"
days left until Christmas!")

Feel free to ask, Thanks,

Gauttam Jada
  • 588
  • 4
  • 7
0

The fix for me was to add the following to the code:

if ( from.getTimezoneOffset( ) !== to.getTimezoneOffset( ) ) {
    if ( from.getTimezoneOffset( ) < to.getTimezoneOffset( ) ) {
        to.setHours( to.getHours( ) - 1 );
    } else if ( from.getTimezoneOffset( ) > to.getTimezoneOffset( ) ) {
        to.setHours( to.getHours( ) + 1 );
    }
}
BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96