简体   繁体   中英

How do I only enable the last days of months with Datepicker Jquery?

So my question is pretty simple. I'd like to have specific dates that are enabled on a datepicker, like this http://tokenposts.blogspot.fr/2011/05/jquery-datepicker-disable-specific.html but I only want the last day of any month, for example 30/06/2012, 31/07/2012, ...

Any clue ?

This is how you can do it...

Getting the last day for a given year and month:

function getLastDayOfYearAndMonth(year, month)
{
    return(new Date((new Date(year, month + 1, 1)) - 1)).getDate();
}

Check with beforeShowDay event if date is the last month day:

$('.selector').datepicker(
{
    beforeShowDay: function(date)
    {
        // getDate() returns the day [ 0 to 31 ]
        if (date.getDate() ==
            getLastDayOfYearAndMonth(date.getFullYear(), date.getMonth()))
        {
            return [true, ''];
        }

        return [false, ''];
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM