0

I have to simply calculate difference between two dates, and display it as integer but my code below gives errors if there are large amount of dates (more than 26) in between 2 dates, as well as if there is a date "31st" of a month in between 2 dates.

Can not find whats wrong with my code...?

The values of 'ires_sakuma_datums' and 'ires_beigu_datums' are set by jquery calendar picker and are displayed in the format e.g. 25-08-2012 and 17-09-2012 respectively and the result should be displayed into id 'dienu_skaits'

Example 'ires_sakuma_datums' is set to 28-08-2012 and 'ires_beigu_datums' is set to 29-09-2012 and it results into 31.041666666666668 although I would expect to result into 32

   function getDays()
    {
        var x = document.getElementById('ires_sakuma_datums').value;
        var y = document.getElementById('ires_beigu_datums').value;

        //assuming that the delimiter for dt time picker is a '-'.
        var arr1 = x.split('-');
        var arr2 = y.split('-');
        var dt1 = new Date();
        dt1.setFullYear(arr1[2], arr1[1], arr1[0]);
        var dt2 = new Date();
        dt2.setFullYear(arr2[2], arr2[1], arr2[0]);

        document.getElementById('dienu_skaits').value = (dt2.valueOf() - dt1.valueOf()) / (60 * 60 * 24 * 1000);

document.forms['test'].elements['dienu_skaits'].focus();
    }
raivis
  • 37
  • 1
  • 2
  • 9
  • 2
    I strongly suggest using DateJs or MomentJs for date manipulation in JavaScript. – Ray Cheng Aug 22 '12 at 19:42
  • 1
    @editor and approvers: no jQuery in sight, adding jQuery and jQuery UI tags is weird. – Dave Newton Aug 22 '12 at 19:47
  • 1
    Your sample code would be better if you removed the references to the DOM, and focused on the date part, makes for less noise, it's irrelevant where the data comes from. Always try to created reductions that only have relevant code – Ruan Mendes Aug 22 '12 at 19:48
  • 1
    You should also specify the errors you are seeing. – Greg Ross Aug 22 '12 at 19:50
  • 1
    @raivis please edit the question with expected results and actual results, don't add it as comments – Ruan Mendes Aug 22 '12 at 19:54
  • Check out this jsfiddle I created, then say what is wrong with the results there http://jsfiddle.net/qB9V3/ – Ruan Mendes Aug 22 '12 at 19:56
  • @raivis result looks normally. Difference is 31 and piece of days. – Arkady Aug 22 '12 at 19:56
  • @Juan Mendes I'm afraid I do not know how to use jsfiddle.net but I will check it out – raivis Aug 22 '12 at 20:08
  • Just click on the link, results show up in your console, use Chrome, or FF with firebug, or IE with dev tools – Ruan Mendes Aug 22 '12 at 20:08
  • I see the problem, if you diff `31-08-2012` and `1-09-2012` you get 0 days. http://jsfiddle.net/qB9V3/2/ but diff between `28-09-2012` and `29-09-2012` is -1 http://jsfiddle.net/qB9V3/3/ – Ruan Mendes Aug 22 '12 at 20:12
  • Duplicate of http://stackoverflow.com/questions/7571977/why-this-operation-with-date-number-of-days-between-2-dates-return-this-value – Ruan Mendes Aug 22 '12 at 20:25

1 Answers1

0

The problem is that months to be passed to Date are 0 based , so you have to subtract 1 from the month. http://jsfiddle.net/qB9V3/5/ Therefore, your comparison between 28-08-2012 and 29-09-2012 was actually diffing 28-09-2012 and 29-10-2012

So your code would need to change to be

    var arr1 = x.split('-');
    var arr2 = y.split('-');
    var dt1 = new Date();
    dt1.setFullYear(arr1[2], parseInt(arr1[1],10) -1, arr1[0]);
    var dt2 = new Date();
    dt2.setFullYear(arr2[2], parseInt(arr2[1],10) - 1, arr2[0]);

Beware that if your dates cross daylight savings changes, the number of days in between won't be even, so you'll need to round the result

document.getElementById('dienu_skaits').value = Math.round( 
    (dt2 - dt1) / (60 * 60 * 24 * 1000) 
);
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • hmm...I just checking your suggestion but if I set for example between `23-08-2012` and `28-10-2012` then result becomes to `66.04166666666667` No clue why it turns into such number...? – raivis Aug 22 '12 at 20:31
  • @raivis That is incorrect, if you use the two dates you've given ( `23-08-2012 `and `28-10-2012`), you'll get 66 days in between, see http://jsfiddle.net/Yabba/. The case where you'll get chopped numbers is if you cross daylight savings changes. For example, if you diff `23-08-2012` and `28-11-2012`, you'll get `-97.04166666666667`, that is what you'd expect since the time changed (Daylight savings end on 2012-11-04 in the US at least). – Ruan Mendes Aug 22 '12 at 21:02
  • @raivis, actually if you're in a place that follows the European Union daylight savings, than the dates you gave do cross daylight saving changes, that's why the numbers aren't even. You can account for that (roughly) by rounding the values with `Math.round` – Ruan Mendes Aug 22 '12 at 21:05
  • Thanks Juan! I also thought its because of daylight savings. – raivis Aug 22 '12 at 21:36