Kendo Detpicker

1 Answer 30 Views
Date/Time Pickers DateInput DateRangePicker
Katie
Top achievements
Rank 1
Katie asked on 16 Mar 2022, 06:06 PM

Hi

I'm trying to try to disable all dates except for the last date of the month for a range. I add the range ok but all dates are enabled. I just need last date of month. I'm using Jquery. Is there a way to do this with the datepicker? 

 


 $(document).ready(function () {
                        $("#datepicker").kendoDatePicker({
                            min: new Date(),
                            min: new Date(),
                            format: "MM/dd/yyyy",
                            type: "date",
                            change: function(e) {
                                  var value = this.value();
                                  var days = value.getDate();
                                  var d = new Date();
                                  d = value;
                                d.setDate(d.getDate() - days + 1, 0);
                                    console.log(d);
                            },
                            value: "d"

                        });
                    });

1 Answer, 1 is accepted

Sort by
0
Georgi Denchev
Telerik team
answered on 21 Mar 2022, 10:05 AM

Hello, Katie,

You can use the disableDates configuration which allows you to pass a function that can be used to disable dates based on a condition.

     // Function to find how many days the month has.
     Date.prototype.monthDays= function(){
        var d = new Date(this.getFullYear(), this.getMonth()+1, 0);
        return d.getDate();
      }

      $("#datePicker").kendoDatePicker({
        value: new Date(),
        disableDates: function (date) {
          var disabled = [];
          // Fill the array with days that should be disabled(all except the last one).
          if(date) {
            var days = date.monthDays();
            for(let i=1;i<days;i++) {
              disabled.push(i);
            }
          }

          // Disable the dates in the array.
          if (date && disabled.indexOf(date.getDate()) > -1 ) {
            return true;
          } else {
            return false;
          }
        }
      });

Source of the monthDays function:

https://stackoverflow.com/a/1185804 

Best Regards,
Georgi Denchev
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Date/Time Pickers DateInput DateRangePicker
Asked by
Katie
Top achievements
Rank 1
Answers by
Georgi Denchev
Telerik team
Share this question
or