user979331
user979331

Reputation: 11871

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
                });

Upvotes: 2

Views: 3842

Answers (4)

Prajapati Hardik
Prajapati Hardik

Reputation: 1

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

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

Upvotes: 0

Luis Paulo Pinto
Luis Paulo Pinto

Reputation: 6036

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

Upvotes: 6

GSKKC
GSKKC

Reputation: 65

We have properties

minDate -> For setting minimum selectable date
startDate -> For setting onload selected date
maxDate -> For setting maximum selectable date

We also have a similar function

setMinDate
setStartDate
setEndDate -> if its datepicker-range
setMaxDate

Upvotes: 0

Thallius
Thallius

Reputation: 2619

You have to set startDate to today and date to today +6 month

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: date,
                    date: defaultDate,
                    changeMonth: true
            });

Upvotes: 5

Related Questions