Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 418
Xuan-Thi Nguyen Avatar asked Oct 07 '22 07:10

Xuan-Thi Nguyen


1 Answers

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, ''];
    }
});
like image 189
Leniel Maccaferri Avatar answered Oct 10 '22 09:10

Leniel Maccaferri