jQuery UI DatePicker - Disable all days except las

2020-06-12 02:44发布

I'm trying to use the jquery UI datepicker to show a calendar with only the last day of the month selectable.

I've successfully used the beforeShowDay event to disable days of the week but not sure how I'd use this to disable everything but the last day of the month.

2条回答
狗以群分
2楼-- · 2020-06-12 02:59
function LastDayOfMonth(Year, Month) { 
    //set next month first day
    //substract one day from it.        
    return new Date(new Date(Year, Month+1,1)-1).getDate(); 
} 

This will work

查看更多
等我变得足够好
3楼-- · 2020-06-12 03:16

beforeShowDay is called for every date shown on the calender. When beforeShowDay is called, you need to determine if the date passed to it is the last day of the month.

The following JScript will return the last day of the month for a given year and month.

function LastDayOfMonth(Year, Month)
{
    return(new Date((new Date(Year, Month+1,1))-1)).getDate();
}

Use the following code to determine if the beforeShowDay's date is the last of the month:

$('.selector').datepicker({
    beforeShowDay: function (date) {
        //getDate() returns the day (0-31)
        if (date.getDate() == LastDayOfMonth(date.getFullYear(),date.getMonth())) {
            return [true, ''];
        }
        return [false, ''];
    }
});
查看更多
登录 后发表回答