Full-Stack freelance Umbraco developer in the UK.

George Phillipson - Freelance Umbraco developer

Disable dates dynamically in jquery datepicker

How to disable holidays in jquery datepicker and Umbraco CMS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Umbraco.Core.Models;
using Umbraco.Web;
using Umbraco.Web.Mvc;
using Web.Helper;

namespace Web.Core.Controllers
{
    public class BookingController : SurfaceController
    {
        public JsonResult GetCurrentBookings()
        {
            List<IPublishedContent> bookingDetails = UmbracoAssignedContentHelper.PageContentForIEnumerableIPublishedContent("bookings").DescendantsOrSelf("year").Where(x => x.GetPropertyValue<bool>("archiveYear") == false).SelectMany(x => x.Children).Where(x => x.GetPropertyValue<bool>("bookingConfirmed")).ToList();

            List<string> bookingModel = new List<string>();

            foreach (var booking in bookingDetails)
            {
                var bookingDatesArrive = booking.GetPropertyValue<DateTime>("startDate");
                var bookingDatesDepart = booking.GetPropertyValue<DateTime>("endDate");

                for (var dt = bookingDatesArrive; dt <= bookingDatesDepart; dt = dt.AddDays(1))
                {
                    bookingModel.Add(dt.ToString("yyyy-MM-dd"));
                }
            }
            return Json(new { BookingArray = bookingModel.ToArray() }, JsonRequestBehavior.AllowGet);
        }
    }
}
$(document).ready(function () {
    //Custom script for datepicker
    var array;
    $.getJSON("/Umbraco/Surface/Booking/GetCurrentBookings", function (data) {
        array = data.BookingArray;

        $("#from").datepicker({
            defaultDate: "+1w",
            changeMonth: true,
            numberOfMonths: 3,
            minDate: 0,
            beforeShowDay: function (date) {
                var string = jQuery.datepicker.formatDate("yy-mm-dd", date);
                return [array.indexOf(string) === -1];
            }
        }).on("change", function () {
            to.datepicker("option", "minDate", getDate(this));
        }),
            $("#to").datepicker({
                defaultDate: "+1w",
                changeMonth: true,
                numberOfMonths: 3,
                minDate: 0,
                beforeShowDay: function (date) {
                    var string = jQuery.datepicker.formatDate("yy-mm-dd", date);
                    return [array.indexOf(string) === -1];
                }
            })
                .on("change", function () {
                    from.datepicker("option", "maxDate", getDate(this));
                });
    });
});

Please enter your comment.