最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Disable Months On MonthDatePicker - Stack Overflow

programmeradmin3浏览0评论

Please look at my fiddle.

I have a monthpicker which only allows users to select a year in advance but what I want is for past months to be disabled and also any months after a year in advance to be disabled but I cant figure out how to get this to work.

Example Scenario Current month is 'October' so for 'Year 2015' months 'Jan to Sept' will be disabled and months 'Nov to Dec' will be disabled for 'Year 2016'

I have tried using minDate: "0" and maxDate: "1y" but they don't 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')
});

Please look at my fiddle.

I have a monthpicker which only allows users to select a year in advance but what I want is for past months to be disabled and also any months after a year in advance to be disabled but I cant figure out how to get this to work.

Example Scenario Current month is 'October' so for 'Year 2015' months 'Jan to Sept' will be disabled and months 'Nov to Dec' will be disabled for 'Year 2016'

I have tried using minDate: "0" and maxDate: "1y" but they don't 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')
});
Share Improve this question asked Oct 5, 2015 at 10:05 murday1983murday1983 4,01617 gold badges61 silver badges110 bronze badges 1
  • maybe having a look at this question will give you an idea? - stackoverflow./questions/21031211/… – TrojanMorse Commented Oct 5, 2015 at 10:11
Add a ment  | 

3 Answers 3

Reset to default 7

DEMO

You can do it with startDate and endDate but try assigning date variable somewhere outside as 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')
});

what I want is for past months to be disabled and also any months after a year in advance to be disabled

I have tried using minDate: "0" and maxDate: "1y" but they don't 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 upto this month and +1y to allow only upto one year.

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

Use this for disabe previous & future months and enable current month & dates.

<script type="text/javascript">
$(document).ready(function () {
    var date = new Date();
    //Create Variable for first day of current month
    var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
    $("#txtDate").datepicker({
        endDate: new Date(),
        startDate: firstDay,
    });
});
</script>
发布评论

评论列表(0)

  1. 暂无评论