jquery datepicker日期计算

时间:2016-01-18 11:25:34

标签: javascript jquery

请检查此JSFIDDLE并帮我解决以下问题。我真的很困惑。

如果我选择第一个日历值是19(星期二)。我需要像20,21,22,23那样的背影(浅色)。

在下面的代码中如果nights = 4 and arrival = 2日历中显示的结果是正确的。但是,如果nights = 4 and arrival = 6日历中显示的结果是错误的。请查看附图。

// hotel nights.. available is 2, 3, 4, 5, 6, 7, 8, 9 nights.
var nights = 4;
// arrival weekday in hotel: monday = 1, tuesday = 2, wednesday = 3, thursday = 4, friday = 5, saturday =6, sunday = 7
var arrival = 2;

nights = 4 and arrival = 2

enter image description here

nights = 4 and arrival = 6

enter image description here

1 个答案:

答案 0 :(得分:0)

最后问题解决了。 Click Here

更新了脚本

function initializeBooking() {
        $('#form_booking').ajaxSubmit({
            target: '#result',
            success: function () {
                $('#result').fadeIn('slow');
            }
        });
        return false;
    }
    // hotel nights.. available is 2, 3, 4, 5, 6, 7, 8, 9 nights.
    var nights = 2;
    // arrival weekday in hotel: monday = 1 ..... sunday = 7
    var arrival = 7;
    // flexbooking = no .. otherwise you can arrive when you want.
    var flex_booking = 'no';

    var couter = arrival + nights;
    var diff   = 0;

    if(couter>7)
        diff = couter-7;


    $(function () {
        cal_start = new Date();
        cal_start.setTime(cal_start.getTime() + (24 * 60 * 60 * 1000 * 1));

        $("#datepicker_begin").datepicker({
            showWeek: true,
            beforeShowDay: function (date) {
                weekday = date.getDay();
                if (weekday == 0)
                    weekday = 7;

                if (flex_booking == 'no') {
                    if (weekday == arrival) {
                        result = [true, "relevant"];        // clickable for the arrival day
                    } else if (weekday > arrival && weekday <= (arrival + nights)) {
                        result = [false, "relevant"];    // red but no clickable for the days you are staying in the hotel
                    } else {
                        result = [false, ""];   // normal days outside the booking range. could be possible nights > 6 that this is not set and everything is red
                    }
                    if(diff>0 && weekday<=diff)
                    {
                        result = [false, "relevant"];
                    }

                } else {
                    result = [true, "relevant"];
                }

                return result;
            },
            altField: "#date_begin",
            altFormat: "@",
            showOtherMonths: false,
            selectOtherMonths: false,
            numberOfMonths: 3,
            dateFormat: "dd.mm.yy",
            firstDay: 1,
            minDate: cal_start,
            maxDate: "+12m",
            onSelect: function () {
                begin = eval($("#date_begin").val());

                start = new Date();
                start.setTime(begin);

                days = nights;

                end = new Date();
                end.setTime(begin + (24 * 60 * 60 * 1000 * days));
                $("#date_end").val(begin + (24 * 60 * 60 * 1000 * days));

                after = new Date();
                after.setTime(begin + (24 * 60 * 60 * 1000 * days) + (24 * 60 * 60 * 1000));

                $("#datepicker_end").val(('0' + end.getDate()).slice(-2) + '.' + ('0' + (end.getMonth() + 1)).slice(-2) + '.' + end.getFullYear());
                $("#nights_span").html(new Date(end - begin) / 1000 / 60 / 60 / 24);


                $("#datepicker_end").datepicker("destroy");
                $("#datepicker_end").datepicker({
                    beforeShowDay: function (date) {
                        return [false, ""];
                    },
                    altField: "#date_end",
                    altFormat: "@",
                    minDate: new Date(end),
                    maxDate: new Date(after),
                    showOtherMonths: false,
                    selectOtherMonths: false,
                    numberOfMonths: 1,
                    dateFormat: "dd.mm.yy",
                    firstDay: 1,
                    onSelect: function (dateText) {
                        initializeBooking();
                    }
                });

                initializeBooking();
            }
        });
    });