jquery本月的第一个星期五

时间:2013-06-10 18:48:47

标签: jquery jquery-ui jquery-ui-datepicker

如何从jQuery Datepicker获得每个月的第一个星期五?我只是在Datepicker上启用星期五,这是完美的工作,但是当新月开始并且日期尚未到达星期五时会出现问题。 Datepicker不应显示当前月份,直到第一个星期五到达,因为所有日期都将被禁用。

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

1 个答案:

答案 0 :(得分: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, ""];
        }
});