4

I have a weird issue with the datepicker in javascript. It has always been working but it breaks on a specific date (25 march 2016) and I don't see what's wrong with it. I do know the code is not optimal.

03/25/2016 to 03/26/2016 is 1 (night) 03/25/2016 to 03/27/2016 is 2 (nights) 03/25/2016 to 03/28/2016 is 2 (nights) << should be 3 03/25/2016 to 03/29/2016 is 3 (nights) << should be 4

The function of the datepicker is to calculate the number of night between to 2 dates. Therefore 1 is substracted from the total.

http://jsfiddle.net/anc7x02g/3/

$(document).ready(function () {
var selector = function (dateStr) {
    var d1 = $('#datepickerln1').datepicker('getDate');
    var d2 = $('#datepickerln2').datepicker('getDate');
    var diff = 1;
    if (d1 && d2) {
        diff = Math.floor((d2.getTime() - d1.getTime()) / 86400000); // ms per day
        diff = diff -1;
    }
    $('#total').val(diff);
}
$("#datepickerln1").datepicker();
$('#datepickerln2').datepicker();
$('#datepickerln1,#datepickerln2').change(selector)
});

What am i doning wrong?

user3193469
  • 114
  • 9
  • What browser are you using? It seems to work fine for me in Chrome. – Joseph Sep 23 '15 at 06:31
  • I use chrome 45 and have the same issue in Firefox. – user3193469 Sep 23 '15 at 06:32
  • FYI: I just tried it again in Chrome (45.0.2454.93) and Firefox (40.0.3) and it still seems to work... http://jsfiddle.net/anc7x02g/3/ – Joseph Sep 23 '15 at 06:35
  • @user3193469 Do you have any other events binding to the input? – K K Sep 23 '15 at 06:38
  • The code I use is a lot more due to setting the firstdate to today and disabling dates in the past., but this jsfiddle has the same result to me. The issue was reported by someone that uses the website, it's not just me. Be sure to select the dates in 2016. In 2015 it doesnt happen. – user3193469 Sep 23 '15 at 06:42
  • @user3193469 But I cannot reproduce this issue in any of the fiddle. Do you have any other bindings which might be affecting the behavior of the input? – K K Sep 23 '15 at 06:53
  • Not sure what's you mean with "other bindings" but with regards to the code, this is on the jsfiddle what fails om my computer. jquery 1.9.1 and jQuery ui 1.9.2, no warp -in – user3193469 Sep 23 '15 at 07:15
  • I tried and when use your date, from 2016, i get same "misscount error", so I wonder, have you taken into account that 2016 is a leap year? – Asons Sep 23 '15 at 07:30
  • I thought of that, but if it was a leap year issue i would expect it to be in januari. With regards to people not having the issue i wonder if it has to do with browser locale. But that seems not to be relevant with regards to the code. – user3193469 Sep 23 '15 at 07:39

1 Answers1

3

Figured it out ... it's the switch between Standard and Daylight Time ... you loose an hour after last Sunday in March, so 25 is before and 28 is after in 2016

And here is how you calculate it:
How to check if the DST (Daylight Saving Time) is in effect and if it is what's the offset?

Community
  • 1
  • 1
Asons
  • 84,923
  • 12
  • 110
  • 165
  • That seems to be the case, in the Netherlands the dayligh savings change on the 27 of march in 2016. I will look some more into it and give the credits. – user3193469 Sep 23 '15 at 07:44
  • If you add a console.log on the d1/d2 date variables to your fiddle, you will see the output from Chrome saying Standard resp. Daylight Time – Asons Sep 23 '15 at 07:46
  • I did not know about the console.log option, just tested it and that will make life a lot easier. Now I can work on a solution, many thanks. – user3193469 Sep 23 '15 at 07:56