jQuery Bootstrap Datepicker - 仅限月份和年份

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14974394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:09:45  来源:igfitidea点击:

Bootstrap Datepicker - Months and Years Only

jquerytwitter-bootstrap

提问by black_belt

I am using bootstrap datepicker and my code is like following, Demo of the code on jsfiddle

我正在使用引导程序日期选择器,我的代码如下所示,jsfiddle上的代码演示

<div class="input-append date" id="datepicker" data-date="02-2012" 
     data-date-format="mm-yyyy">

 <input  type="text" readonly="readonly" name="date" >    
 <span class="add-on"><i class="icon-th"></i></span>      
</div>      


$("#datepicker").datepicker({
        viewMode: 'years',
         format: 'mm-yyyy'
    });

The above code is working fine, but what I am trying to do is allow users pick months and years only.

上面的代码工作正常,但我想做的是只允许用户选择几个月和几年。

Could you please tell me how to do that?

你能告诉我怎么做吗?

回答by Iswanto San

How about this :

这个怎么样 :

$("#datepicker").datepicker( {
    format: "mm-yyyy",
    viewMode: "months", 
    minViewMode: "months"
});

Reference : Datepicker for Bootstrap

参考:用于 Bootstrap 的日期选择器



For version 1.2.0 and newer, viewModehas changed to startView, so use:

对于 1.2.0 及更新版本,viewMode已更改为startView,因此请使用:

$("#datepicker").datepicker( {
    format: "mm-yyyy",
    startView: "months", 
    minViewMode: "months"
});

Also see the documentation.

另请参阅文档

回答by Tanya

I'm using version 2(supports both bootstrap v2 and v3) and for me this works:

我正在使用版本 2(同时支持 bootstrap v2 和 v3),对我来说这有效:

$("#datepicker").datepicker( {
    format: "mm/yyyy",
    startView: "year", 
    minView: "year"
});

回答by Sheharyar

For the latest bootstrap-datepicker(1.4.0 at the time of writing), you need to use this:

对于最新版本bootstrap-datepicker(撰写本文时为 1.4.0),您需要使用以下命令:

$('#myDatepicker').datepicker({
    format: "mm/yyyy",
    startView: "year", 
    minViewMode: "months"
})

Source: Bootstrap Datepicker Options

资料来源:Bootstrap Datepicker 选项

回答by vishal kumar Saxena

I am using bootstrap calender for future date not allow with allow change in months & year only..

我正在使用 bootstrap calender 作为未来日期不允许只允许在月份和年份中更改..

var j = jQuery.noConflict();
        j(function () {
            j(".datepicker").datepicker({ dateFormat: "dd-M-yy" }).val()
        });

        j(function () {
            j(".Futuredatenotallowed").datepicker({
                changeMonth: true,
                maxDate: 0,
                changeYear: true,
                dateFormat: 'dd-M-yy',
                language: "tr"
            }).on('changeDate', function (ev) {
                $(this).blur();
                $(this).datepicker('hide');
            }).val()
        });

回答by Nisarg Bhavsar

You should have to add only minViewMode: "months"in your datepicker function.

你应该只minViewMode: "months"在你的日期选择器函数中添加。

回答by Marco Puenayan

Para visualiar los meses del a?o actual,visualiza solo los meses, y con el formato solo toma los meses de ese a?o. tambien se puede confirurar para que se visualice una cierta cantidad de meses

Para visualiar los meses del a?o actual,visualiza solo los meses, y con el formato solo toma los meses de ese a?o. tambien se puede confirurar para que se visualice una cierta cantidad de meses

<script>
('#txtDateMMyyyy').datetimepicker({
    format: "mm/yyyy",
    startView: "year",
    minView: "year"
}).datetimepicker("setDate", new Date());
</script>

回答by Kiel

Why not call the $('.input-group.date').datepicker("remove");when the select statement is changed then set your datepicker view then call the $('.input-group.date').datepicker("update");

为什么不在$('.input-group.date').datepicker("remove");更改选择语句时调用然后设置日期选择器视图然后调用$('.input-group.date').datepicker("update");