如何在输入Bootstrap Datepicker中显示多年?

时间:2012-10-29 19:57:53

标签: javascript twitter-bootstrap datepicker

我使用以下代码仅显示年份:

$("#datepicker").datepicker( {viewMode: "years", minViewMode: "years"});

但输入格式为"dd/mm/yyyy"

我怎么能解决这个问题,因为我没有格式"yyyy"

6 个答案:

答案 0 :(得分:65)

试试这个

$("#datepicker").datepicker({
    format: "yyyy",
    viewMode: "years", 
    minViewMode: "years"
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.4/js/bootstrap-datepicker.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.4/css/bootstrap-datepicker.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>


<input type="text" id="datepicker" />

$("#datepicker").datepicker( {
    format: " yyyy", // Notice the Extra space at the beginning
    viewMode: "years", 
    minViewMode: "years"
});

答案 1 :(得分:8)

对于bootstrap datetimepicker,请指定decade值,如下所示:

$(".years").datetimepicker({
    format: "yyyy",
    startView: 'decade',
    minView: 'decade',
    viewSelect: 'decade',
    autoclose: true,
});

答案 2 :(得分:2)

$("#year").datepicker( {
    format: "yyyy",
    viewMode: "years", 
    minViewMode: "years"
}).on('changeDate', function(e){
    $(this).datepicker('hide');
});

答案 3 :(得分:2)

用于bootstrap 3 datepicker。 (注意大写字母)

$("#datetimepicker").datetimepicker( {
  format: "YYYY",
  viewMode: "years"
});

答案 4 :(得分:0)

format: "YYYY" 

应该是资本而不是“yyyy”

答案 5 :(得分:0)

始终为bootstrap 3 datetimepicker的年份 https://eonasdan.github.io/bootstrap-datetimepicker/

   $('#year').datetimepicker({
        format: 'YYYY',
        viewMode: "years",
    });

    $("#year").on("dp.hide", function (e) {
        $('#year').datetimepicker('destroy');
        $('#year').datetimepicker({
            format: 'YYYY',
            viewMode: "years",
        });
    });