Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datepicker minDate with StartDate

What I am trying to do with datepicker is when it first shows, display the current date plus 6 months, which you can do with startDate, but the month back button is disabled. I tried to set a minDate for todays date, same deal, month back button is disabled.

Is there away to have the start date to be the current date plus 6 months, but allow users to click the month back button and not allowed to go past the current date.

var date = new Date();
                date.setDate(date.getDate() - 1);
                var defaultDate = new Date();
                defaultDate.setMonth(date.getMonth() + 6);

                $('[data-toggle="datepicker"]').datepicker({
                        format: 'dd/MM/yyyy',
                        autoHide: true,
                        minDate: date,
                        startDate: defaultDate,
                        changeMonth: true
                });
like image 606
user979331 Avatar asked Feb 07 '26 19:02

user979331


1 Answers

To accomplish what you want you just need to set:

startDate: with the current date, so users will not be allowed to go past this date. [docs reference for startDate]

AND

date: with the date that you want to show as default in the datepicker. [docs reference for date]

Also looks like the datepicker plugin that you are using doesn't have these 2 properties: minDate and changeMonth. So there's no need to use it here.

 var date = new Date();
 
 date.setDate(date.getDate() - 1);
 var defaultDate = new Date();
 defaultDate.setMonth(date.getMonth() + 6);

 $('[data-toggle="datepicker"]').datepicker({
  format: 'dd/MM/yyyy',
  autoHide: true,
  date: defaultDate,
  startDate: date,
 });

You can check all options for this plugin here.

Also, you can simulate it here

like image 68
Luis Paulo Pinto Avatar answered Feb 09 '26 08:02

Luis Paulo Pinto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!