0

如何从 jQuery Datepicker 获取每个月的第一个星期五?我只是在 Datepicker 上启用 Friday's,它工作得很好,但是当新月份开始并且日期还没有达到星期五时,问题就出现了。Datepicker 不应该在第一个星期五到来之前显示当前月份,因为所有日期都将被禁用。

$("#dp").datepicker({
        changeMonth: true,
        changeYear: true,
        minDate: '03/01/2013',
        maxDate: 0M,
        beforeShowDay:
            function (dt) {
                return [dt.getDay() == 5, ""];
            }
    });
4

1 回答 1

0

我想出了解决方案。这里是。

    //var currentDate = new Date('07/04/2013');
    //var currentDate = new Date('06/06/2013');
    //var currentDate = new Date('05/06/2013');
    //var currentDate = new Date('05/03/2013');
    //var currentDate = new Date('05/02/2013');

    var currentDate = new Date();
    var lastFriday;

    if (currentDate.getDay() == 5 || currentDate.getDate() >= 7) {
        lastFriday = currentDate.format("MM/dd/yyyy");
    }
    else if (currentDate.getDate() < 7) {

        var days = new Array();
        var isFridayPast = false;
        var x = 0;
        var gd = currentDate.getDate();

        for (var i = 7; i > 0; i--) {
            days[x] = gd--;                
            var dayName = new Date(currentDate.getFullYear(), currentDate.getMonth(), days[x]).toDateString().substring(0, 3);                
            if (dayName == "Fri") {
                isFridayPast = true
                lastFriday = new Date(currentDate.getFullYear(), currentDate.getMonth(), days[x]).format("MM/dd/yyyy");                    
                break;
            }
        }
        if (isFridayPast == false) {
            lastFriday = new Date(currentDate.getFullYear(), currentDate.getMonth(), -(7 - currentDate.getDay())).format("MM/dd/yyyy");                
        }
    }

 $("#dp").datepicker({
    changeMonth: true,
    changeYear: true,
    minDate: '03/01/2013',
    maxDate: lastFriday,
    beforeShowDay:
        function (dt) {
            return [dt.getDay() == 5, ""];
        }
});
于 2013-06-11T13:41:13.747 回答