JavaScript代码返回NaN而不是数值

时间:2015-05-19 13:10:00

标签: javascript jquery

当我将下面的代码输入jsfiddle时,它完全符合我的要求。但是,当我将其实现到我的项目中时,该值将返回为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>

我添加了下面的代码,认为它会处理NaN,但它还没有奏效。

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

jsfiddle链接是JsFiddle

1 个答案:

答案 0 :(得分:1)

这一行在这里:

 var diff = endDate - (startDate + 1);

导致问题。在你的小提琴上工作

var diff = endDate - startDate;

这导致了这个问题,因为endDate和startDate是对象而你试图用一个数字连接一个对象