显示每个月的假期列表

时间:2014-12-05 07:40:57

标签: javascript jquery

我使用此代码在网页中显示日历:

<script>
                $('#calendar').datepicker({
                inline: true,
                firstDay: 1,
                showOtherMonths: true,
                dayNamesMin: ['Sun','Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
            });
 </script>

现在我想显示每个月的假期列表。所以我使用了这段代码:

<script>

    var unavailableDates = ["16-10-2013", "4-11-2013", "25-12-2013"];

   function unavailable(date) {
    dmy = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();
    if ($.inArray(dmy, unavailableDates) == -1) {
        return [true, ""];
    } else {
        return [false, "", "Holiday"];
    }
}

$(function() {
    $("#calendar-dep").datepicker({numberOfMonths: 2, showCurrentAtPos: 0,minDate: 0,
        dateFormat: 'dd MM yy',
        beforeShowDay: unavailable
    });

      $("#calendar-return").datepicker({numberOfMonths: 2, showCurrentAtPos: 0,minDate: 0,
        dateFormat: 'dd MM yy',
        beforeShowDay: unavailable
    });

})


   </script>

但是没有显示假日列表。我怎样才能编写更合适的代码?

2 个答案:

答案 0 :(得分:0)

    dmy = date.getDay() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear(); //Use GetDay() instead of Getdate()

或您可以使用此

    function unavailable(date) {
      var day= date.substring(0, 2);
        var month = date.substring(3, 5);
        var year = date.substring(6, 10);

        var myDate = new Date(year, month - 1, day);
if ($.inArray(myDate , unavailableDates) == -1) {
    return [true, ""];
} else {
    return [false, "", "Holiday"];
}
}

答案 1 :(得分:0)

date.getDate()返回Javascripts Date实例。是否要使用这种比较:

var d = date.getDate(); // plain javascript Date object
dmy = d.getDay() + '-' + (d.getMonth() + 1) + '-' +  d.getFullYear();

请注意,这里的月份可能有1位或2位数字。希望这会有所帮助。