How to disable the past dates in the Kendo date picker?
Asked Answered
S

3

5

How to disable the past dates in the Kendo date picker ? ( Date Picker validation)

That will allow the user to select only the current date and future date.

In the HTML :
@Html.EditorFor(Model => Model.AppointmentDate)

In the JQuery :
$('#AppointmentDatee').data('kendoDatePicker')
Speiss answered 20/9, 2018 at 3:45 Comment(5)
Can you provide snippet of the Kendo date picker code? At least it should comply with minimal reproducible example.Zoosperm
@TetsuyaYamamoto, sorry for that. please take a look on the syntaxSpeiss
I think easier to use Html.Kendo().DatePicker().Min(DateTime.Now) or Html.Kendo().DatePicker().Min(DateTime.Today). Also I want to ask this: are you want to hide all disabled past dates or just show but disable click on them?Zoosperm
Either way , it is fine. Objective is to select either today’s or future date. Not the past date. I was hoping to make the changes on html editor . Having kendo datepicker syntax actually changes the date format or design format.Speiss
I decided to include both of them (hiding all past dates & grayed-out disabled dates). The implementation parts are somewhat similar, just with little difference.Zoosperm
Z
14

The shortest way to disable past dates is using min parameter with current date value:

var presentDate = new Date();

$(function () {
    var datepicker = $('#AppointmentDate').kendoDatePicker({
        value: presentDate,
        min: presentDate,
    }).data('kendoDatePicker');
});

If you're using Razor with @Html.Kendo() helper, use DatePickerBuilderBase.Min() method:

@(Html.Kendo().DatePicker().Name("AppointmentDate").Min(DateTime.Today))

However, the min parameter will remove all disabled past dates (i.e. they're not shown in calendar view). If you want to show disabled dates but the user cannot interact with them (by clicking the date), use k-state-disabled CSS class in empty option inside month parameter:

var datepicker = $('#AppointmentDate2').kendoDatePicker({
        value: presentDate,
    min: presentDate,
    month: {
        empty: '<div class="k-state-disabled">#= data.value #</div>'
    }
}).data('kendoDatePicker');

If @(Html.Kendo()) helper is used, use DisabledDates to call a function which disables past dates like example below:

<script>
var getPastDates = function(begin, end) {
    for (var dtarr = [], date = start; date < end; date.setDate(date.getDate() + 1)) {
        dtarr.push(new Date(dt));
    }

    return dtarr;
}

function disablePastDates(date) {
    var pastDate = getPastDates(new Date('0001-01-01T00:00:00Z'), new Date());
    if (date && compareDates(date, dates)) {
        return true;
    } 
    else {
        return false;
    }
}

function compareDates(date, dates) {
    for (var i = 0; i < dates.length; i++) {
        if (dates[i].getDate() == date.getDate() &&
            dates[i].getMonth() == date.getMonth() &&
            dates[i].getYear() == date.getYear()) {
            return true;
        }
    }
}
</script>

Helper usage:

@(Html.Kendo().DatePicker().Name("AppointmentDate").DisableDates("disablePastDates"))

Working examples:

JSFiddle demo 1 (hidden past dates)

JSFiddle demo 2 (grayed-out past dates)

References:

Kendo.Mvc.UI.Fluent.DatePickerBuilderBase.Min(DateTime)

Show Out-of-Range Dates as Disabled

Kendo MVC DatePicker - Disable dates

Similar issue (with different approach):

How to disable past dates without hiding them in Kendo date picker?

Zoosperm answered 20/9, 2018 at 6:55 Comment(1)
Best well detailed explanation;Speiss
T
0

if you use jquery for your kendoDatePicker , this code may help you!

$("#MyDatapickerElement").kendoDatePicker({
        value: new Date(),
        disableDates: function (date) {
            if (date <= new Date()) {
                return true;
            } else {
                return false;
            }
        }
    });
Threshold answered 16/3, 2020 at 7:31 Comment(0)
C
0

If using Html.Kendo().DatePicker() you can show the disabled dates using the MonthTemplate. Example below shows the Minimum Date set to DateTime.Today and sets the MonthTemplate to show past dates as disabled.

Html.Kendo().DatePicker()
            .Name("MyDate")
            .Min(DateTime.Today)
            .MonthTemplate(m=>m
            .Empty("<div class=\"k-state-disabled\">#= data.value #</div>")
             )
Contortion answered 23/4, 2021 at 14:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.