Search code examples
javascriptjqueryjquery-uidatetimepicker

Counting days in jQuery datepicker - one day out for a particular week


I've got two datetimepickers (as found at http://trentrichardson.com/examples/timepicker) and the number of weekdays between them is calculated. This seems to work fine except for one particular week.

For example, if you choose 15/09/2014 in the first box and 22/09/2014 in the second, it will calculate 5 days as there are 5 weekdays between them. Great!

However, choose 20/10/2014 and 27/10/2014 and it will calculate 4. Also, a choice of 24/10/2014 and 27/10/2014 says that there is no difference between the dates.

I've tried lots of little changes in the code (such as changing how the weekends are figured out and suchlike) but it's a strange one as it works in every case I've tried except for that week so it's like I'm trying to fix code that works already.

I don't understand why this is happening, do you?

JS Fiddle of this: http://jsfiddle.net/xxbjL40q/

HTML

<h4>When are you off from?</h4>
<div class="form-group">
    <input type="text" class="form-control" id="firstday" name="firstday">
</div>

<h4>When are you back in the office?</h4>
<div class="form-group">
    <input type="text" class="form-control" id="backday" name="backday">
</div>

<p id="daysoffmessage">Please choose a day from both boxes. If you update your 'from' date after your 'back' date, please reselect your 'back' date to correct the day count.</p>

<input type="text" id="daycount" name="daycount">

JS

$('#firstday').datetimepicker({
    hourMin: 0,
    hourMax: 12,
    stepHour: 12,
    showMinute: false,
    showTime: false,
    dateFormat: 'dd/mm/yy',
    constrainInput: true,
    beforeShowDay: $.datepicker.noWeekends,
    firstDay: 1
});

$('#backday').datetimepicker({
    hourMin: 0,
    hourMax: 12,
    stepHour: 12,
    showMinute: false,
    showTime: false,
    dateFormat: 'dd/mm/yy',
    constrainInput: true,
    beforeShowDay: $.datepicker.noWeekends,
    firstDay: 1,
    onSelect: function(){
        // get the stuff
        var firstday = $('#firstday').datepicker('getDate');
        var backday = $('#backday').datepicker('getDate');

        // sort the weekends out
        var weekend_count = 0;
        for (i = firstday.valueOf(); i <= backday.valueOf(); i+= 86400000) {
            var temp = new Date(i);
            if (temp.getDay() == 0 || temp.getDay() == 6) {
                weekend_count++;
            }
        }

        // how many weekdays?
        var total = ((backday - firstday) / 86400000) - weekend_count;
        var total_1dp = total.toFixed(1);

        // put the info where I need it
        $('#daycount').val(total_1dp);
    }
});

Solution

  • Update. So, as @ScottBrown has said, workaround is to change hourMin from 0 to, for example, 6. It helps to get rid of incorrect behaviour (counting Sunday twice) during changing of "Daylight Standard Time" to "Daylight Savings Time", even though it's not an universal solution.

    Fiddle.

    ... and my modified fiddle with handling error during selecting only second date and with recalculating value if the first date is changed.