Disable months by month / DatePicker

Take a look at my fiddle .

I have a month ticker that allows users to select a year in advance, but I want the last months to be turned off, and also a few months after a year to be turned off, but I canโ€™t figure out how to do this.

Example scenario The current month is October, so for the โ€œyear 2015โ€ the months โ€œJanuary to Septemberโ€ will be disabled and the months from November to December will be disabled for โ€œ2016โ€

I tried using minDate: "0" and maxDate: "1y", but they do not work.

HTML

<div class="input-group date" style="width: 200px"> <input type="text" id="example1" class="form-control" style="cursor: pointer"/> <span class="input-group-addon"> <i class="glyphicon glyphicon-calendar"></i> </span> </div> 

JQuery

 $('#example1').datepicker ({ format: "MM yyyy", minViewMode: 1, autoclose: true, startDate: new Date(new Date().getFullYear(), '0', '01'), endDate: new Date(new Date().getFullYear()+1, '11', '31') }); 
+5
source share
2 answers

Demo

You can do this with startDate and endDate , but try assigning the date variable somewhere outside, as shown below:

 var date=new Date(); var year=date.getFullYear(); //get year var month=date.getMonth(); //get month $('#example1').datepicker ({ format: "MM yyyy", minViewMode: 1, autoclose: true, startDate: new Date(year, month, '01'), //set it here endDate: new Date(year+1, month, '31') }); 
+5
source

I want the last months to be disconnected, and also for a few months in a year to be disconnected.

I tried using minDate: "0" and maxDate: "1y", but they do not work.

You are on the right track. But instead of minDate and maxDate use startDate and endDate . Like this:

 $('#example1').datepicker ({ startDate: "-0m", endDate: "+1y", ... }); 

-0m to allow only up to this month and +1y to allow only up to one year.

Fiddle: http://jsfiddle.net/abhitalks/RWY2X/34/

+3
source

Source: https://habr.com/ru/post/1232963/


All Articles