Muhammad Danish Khan
Muhammad Danish Khan

Reputation: 459

minDate:0 in Jquery DatePicker also disable today's date

Sorry for posting this simple question. Application is going to be deployed soon, and recently a bug came, but i am not able to figure out why it is comming.

Created A JS_BIN this is the link

http://jsbin.com/razufavuru/1/edit?html,js,output

Error is minDate: 0 is disabling today's date also while i want to disable only past dates.

This is jquery code:

$("#leaveEndDate").datepicker(
    {
        minDate: 0,
        beforeShowDay : disablePublicHolidaysAndWeekends,
        minDate : dateToday,
        onSelect : function(dateText, inst) {
            var firstDate = $("#leaveStartDate").datepicker('getDate');
            var selDate = null;
            selDate = $(this).datepicker('getDate');
            var lastDate = selDate;

            if (lastDate < firstDate) {
                $("#leaveEndDate").val('');
                $("#endDateError").html(
                        "End Date must be greater than Start Date");
                $(inst).datepicker('show');
            } else {
                $("#endDateError").html("");
                calculateDays(firstDate, lastDate);
            }

        }
    });

Disable Holiday and Weekends Method is

function disablePublicHolidaysAndWeekends(date) {
 var month = date.getMonth();
 var day = date.getDate();
 var year = date.getFullYear();
if(day < 10 && day > 0){
    day = '0'+day;
}
var getdate = year+ '/' + '0' +(month + 1) + '/' + day;
for (var i = 0; i < publicHolidayDates.length; i++) {
    if ($.inArray( getdate ,publicHolidayDates) != -1 || new Date() > date) {
        return [ false ];
    }
}

var noWeekend = $.datepicker.noWeekends(date);

return !noWeekend[0] ? noWeekend : [ true ];

}

this is the img

enter image description here

public holidays are comming from database.

It confuse me lot when this code for another datapicker works.

$("#targetDate").datepicker({
   beforeShowDay : $.datepicker.noWeekends,
   minDate : 0
 });

Here it do not disable today date. this is the img

enter image description here

Upvotes: 0

Views: 471

Answers (1)

Yuri
Yuri

Reputation: 3284

jQuery Datepicker value minDate expect value of type

Type: Date or Number or String

Try minDate: new Date() instead, or minDate: '0'

UPDATE:

What causes your issue is your custom function:

function disablePublicHolidaysAndWeekends(date) {
    var month = date.getMonth();
    var day = date.getDate();
    var year = date.getFullYear();
    if(day < 10 && day > 0){
        day = '0'+day;
    }
  if(month < 9 && month > 0){ //added control over month, as you would have got errors for month=10, 11, 12 in the below variable getdate
     month = '0' +(month + 1);
  }
  else{
    month += 1;
  }
    var getdate = year+ '/' + month + '/' + day;
    for (var i = 0; i < publicHolidayDates.length; i++) {
        //removed "|| new Date() > date"
        if ($.inArray( getdate ,publicHolidayDates) !== -1 ) {
            return [ false ];
        }
    }

    var noWeekend = $.datepicker.noWeekends(date);

    return !noWeekend[0] ? noWeekend : [ true ];
}

Read this about Date comparison in javascript Fiddle here

Upvotes: 1

Related Questions