John Britto
John Britto

Reputation: 457

Bootstrap datepicker beforeShowDay works only after first click

I included the bootstrap datepicker in my form. the beforeShowDay() is working only after the first click on the date or the previous month arrow(>>). Can you please help me so that it works on open itself.

Here is the code i tried.

    $(document).ready(function(){

            $('#ReportFromDate').datepicker({
                autoclose:true,
                endDate: new Date(),
                format: "mm/dd/yyyy",
                language : 'pt-BR'
            }); 


            $('#ReportToDate').datepicker({
                autoclose:true,
                beforeShowDay: function (date) {
                    var endDate1 = new Date();
                    var startDate1 = new Date($('#ReportFromDate').val());
                    console.log("new "+ startDate1 +" "+endDate1);
                    if (date.valueOf() <= endDate1.valueOf() && date.valueOf() >= startDate1.valueOf()) {
                        return (date.valueOf() <= endDate1.valueOf() && date.valueOf() >= startDate1.valueOf());
                    } else {
                        return false;
                    }
                },
                format: "mm/dd/yyyy"
            });
   });

Upvotes: 0

Views: 1362

Answers (1)

CodeBob
CodeBob

Reputation: 785

I would add the function outside of the datepicker itself. There may be an issue on the order that the code is executed, and values that are needed to calculate the date.

There are some helpful solutions/ examples here.

Upvotes: 0

Related Questions