SO - 15400775

by Zareh Geertjes

HTML

<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/themes/redmond/jquery-ui.css">
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"></script>
<input />

JavaScript

var startDates = '18-01-2016,02-02-2016,13-04-2016';
var arrayAsDateObjects = convertStringToDateObject(startDates);

$('input').datepicker({
    dateFormat: 'dd-mm-yy',
    beforeShowDay: function (date) {
    		var array = startDates.split(',');
        var string = jQuery.datepicker.formatDate('dd-mm-yy', date);
        return [array.indexOf(string) == -1]
    },
    onSelect: function (date) {
        alert(findNextDisabledDateWithinMonth(date));
    }
});

function findNextDisabledDateWithinMonth(date) {
    var splitDate = date.split("-");
    var selectedDate = new Date(splitDate[2], Number(splitDate[1]) - 1, splitDate[0]);
    var nextDisabledDate = null;
    $.each(arrayAsDateObjects, function (i, ele) {
        if (selectedDate < ele) {
            nextDisabledDate = ele;
            return false;
        }
    });
    return nextDisabledDate;
}

function convertStringToDateObject(dateString) {
    var ls = [];
    var array = dateString.split(',');
    $.each(array, function (i, ele) {
        var splitDate = ele.split("-");
        var date = new Date(splitDate[2], Number(splitDate[1]) - 1, splitDate[0]);
        ls.push(date);
    });
    // Sort the dates in ascending order
    
    ls.sort(function (a, b) {
        return a - b;
    });
    return ls;
}