0

When I inputted the code below into jsfiddle it worked exactly as I wanted. However when I implemented it into my project the value returns as NaN.

<script type="text/javascript">
    $(function () {
        $('#datepicker8').datepicker({
            showOnFocus: false,
            showTrigger: '#calImg',
            beforeShowDay: $.datepicker.noWeekends,
            pickerClass: 'noPrevNext',
            dateFormat: "dd-mm-yy", changeMonth: true, changeYear: true,
            onSelect: function (dateStr) {
                var min = $(this).datepicker('getDate');
                $('#datepicker9').datepicker('option', 'minDate', min || '0');
                datepicked();
            }
        });
        $('#datepicker9').datepicker({
            showOnFocus: false,
            showTrigger: '#calImg',
            beforeShowDay: $.datepicker.noWeekends,
            pickerClass: 'noPrevNext',
            dateFormat: "dd-mm-yy", changeMonth: true, changeYear: true,
            onSelect: function (dateStr) {
                var max = $(this).datepicker('getDate');
                $('#datepicker8').datepicker('option', 'maxDate', max || '+1Y');
                datepicked();
            }
        });
    });


    var datepicked = function () {
        var from = $('#datepicker8');
        var to = $('#datepicker9');
        var nights = $('#CalcDate1');

        var startDate = from.datepicker('getDate');
        startDate.setDate(startDate.getDate() + 1);

        var endDate = to.datepicker('getDate')


        // Validate input
        if (endDate && startDate) {


            // Calculate days between dates
            var millisecondsPerDay = 86400 * 1000; // Day in milliseconds
            startDate.setHours(0, 0, 0, 1);  // Start just after midnight
            endDate.setHours(23, 59, 59, 999);  // End just before midnight
            var diff = endDate - (startDate + 1);  // Milliseconds between datetime objects    
            var days = Math.ceil(diff / millisecondsPerDay);

            // Subtract two weekend days for every week in between
            var weeks = Math.floor(days / 7);
            var days = days - (weeks * 2);

            // Handle special cases
            var startDay = startDate.getDay();
            var endDay = endDate.getDay();

            // Remove weekend not previously removed.   
            if (startDay - endDay > 1)
                var days = days - 2;

            // Remove start day if span starts on Sunday but ends before Saturday
            if (startDay == 0 && endDay != 6)
                var days = days - 1

            // Remove end day if span ends on Saturday but starts after Sunday
            if (endDay == 6 && startDay != 0)
                var days = days - 1

            nights.val(days);
        }
    }
</script>

I added the code below thinking that it would deal with NaN but it hasn't worked.

if (!isNaN(days)) {
    document.getElementById('CalcDate1').value = days;
}
else {
    document.getElementById('CalcDate1').value = "";
}

The jsfiddle link is JsFiddle

8
  • did you include both jquery and jquery ui? Commented May 19, 2015 at 13:15
  • @briosheje I have both included as far as I can tell Commented May 19, 2015 at 13:17
  • what value returns as NaN? Commented May 19, 2015 at 13:17
  • Can you detect the line where the error is being thrown? Usually, in the console, it tells you the exact line, if you could tell us what that line is it would be great! Commented May 19, 2015 at 13:18
  • 2
    please check, var diff = endDate - (startDate + 1); console.log(diff); is 'diff' calculate properly? Commented May 19, 2015 at 13:24

1 Answer 1

1

Its this line here:

 var diff = endDate - (startDate + 1);

that is causing the issue. On your fiddle where its working

var diff = endDate - startDate;

This is causing the issue because endDate and startDate are objects and you are trying to concatenate an object with a number

Sign up to request clarification or add additional context in comments.

4 Comments

I had to add (startDate + 1) as the user requsted it. They want their value to show as 0 if datepicker8 and datepicker9 are chosen as the same day
That maybe so, but adding in +1 is what has caused your issue.
In your fiddle there isnt an issue with this. If the same date is chosen it comes back as 0
Thanks craicerjack that worked, I had two + 1 added and hadn't spotted that second one

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.