Search code examples
javascriptcalendaryui3

How do I disable dates previous to YUI3 Calendar minimumDate?


According to the documentation, setting the minimumDate of a calendar will "reset any date any earlier date to this date", but I would like to disable the selection of any previous dates. The control disables the "previous" month button for the month of the minimum date, but still allows selection of earlier dates.


Solution

  • Not having found a solution after some searching, I am posting one I finally came up with. Using the calendar's disabled dates rules, I created the following:

    YUI().use('calendar', function (Y) {
    
        var calendar, rules,
            settings = { contentBox: '#dueDateSelect', minimumDate: new Date() };
    
        function getDisabledDatesRule(date, name) {
            var year = date.getFullYear(), 
                month = date.getMonth(), 
                daybefore = date.getDate() - 1, 
                r = {};
            r[year] = {};
            r[year][month] = {};
            r[year][month]['1-' + daybefore] = name;
            return r;
        }
    
        if (settings.minimumDate) {
            settings.disabledDatesRule = 'days-before-min';
            rules = getDisabledDatesRule(
                settings.minimumDate, 
                settings.disabledDatesRule
            );
        }
    
        calendar = new Y.Calendar(settings).render();
    
        if (rules) {
            calendar.set("customRenderer", {
                rules: rules
            });
        }
    
    });