开发者

Enable first sunday in Jquery UI datapicker

开发者 https://www.devze.com 2023-03-18 04:31 出处:网络
I\'m currently working on a Jquery datepicker where holidays are disabled and all sundays, except for the first one in each month. I have been trying to play around a little with the code, and found o

I'm currently working on a Jquery datepicker where holidays are disabled and all sundays, except for the first one in each month. I have been trying to play around a little with the code, and found out how to disable all sundays and holidays, but i cant figure out how to enable the first sunday of evey month.

Currently my code looks like this:

<script type="text/javascript">
        (function(){

            var natDays = [[12, 24],[12, 25], [1,1], [12, 31]];
            var daysToDisable = [0];
            function nationalDays(date) {
                for (i = 0; i < natDays.length; i++) {
                    if (date.getMonth() == natDays[i][0] - 1
                    && date.getDate() == natDays[i][1]) {
                        return [false, natDays[i][2] + '_day'];
                    }
                }
                for (i = 0; i < daysToDisable.length; i++) {
                    if ($.inArray(day, daysToDisable) != -1) {
                        return [false];
                    }
                }
                return [true];
            }   
            // Datepicker
            $('#datepicker').datepicker({
                inlin开发者_如何学Goe: true,
                firstDay: 1,
                changeYear: true,
                changeMonth: true,
                beforeShowDay: nationalDays,
            });
        });
</script>


Logically, the first Sunday of the month is always on or before the 7th and the second (and subsequent) Sundays are after the 7th.

function nationalDays(date) {
    for (i = 0; i < natDays.length; i++) {
        if (date.getMonth() == natDays[i][0] - 1 && date.getDate() == natDays[i][1]) {
            return [false, natDays[i][2] + '_day'];
        }
    }
    if (date.getDate() > 7 && $.inArray(date.getDay(), daysToDisable) != -1)
        return [false];
    }
    return [true];
}

I would also suggest to change the structure of your natDays array to a flat array in order to speed up lookups. For your class prefixes (which are not set in your example), you can use an extra array with matching indices. Your final function would look like this:

var natDays = ["12-24", "12-25", "1-1", "12-31"];
var classPrefixes = ["", "", "", ""];
var daysToDisable = [0];

function nationalDays(date) {
    var index = $.inArray((date.getMonth() + 1) + "-" + date.getDate(), natDays);
    if (index != -1) {
        return [false, classPrefixes[index] + '_day'];
    }
    if (date.getDate() > 7 && $.inArray(date.getDay(), daysToDisable) != -1)
        return [false];
    }
    return [true];
}


The method you're looking for is date.getDay(), which will return a number from 0 to 6, with 0 being Sunday.

function nationalDays(date) {
    if(date.getDay() == 0) {
        // do stuff...
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号