简体   繁体   English

Bootstrap Datepicker仅显示日期

[英]Bootstrap datepicker show dates only

Is it possible to show only dates in Bootstrap datepicker ? 是否可以在Bootstrap datepicker中仅显示日期

I am showing only months and years using following methods - 我仅使用以下方法显示月份和年份-

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

$("#month").datepicker({
    format: " mm", 
    viewMode: "months",
    minViewMode: "months",
    maxViewMode: 0
});

A live demo showing only year and month selection in Year and Month field. 一个实时演示 ,仅在“ 月”字段中显示年和月选择。 I want to show only date from 1 to 31 without Month, Year at the top and also without day's name, at the Date field. 我只想显示1到31之间的日期 ,而在“ 日期”字段中则不显示月份,顶部,而要显示月份。

Yes, it is possible, but a bit complicated, you have to: 是的,可以,但是有点复杂,您必须:

  • Set the format option to d (or dd ) format选项设置为d (或dd
  • Set maxViewMode option to 0 ( minViewMode is 0 by default) maxViewMode选项设置为0 (默认情况下minViewMode0
  • Hide prev/next month buttons and the month name button, you can do it using CSS: 隐藏上一个/下一个月份按钮和月份名称按钮,您可以使用CSS进行操作:
    • the days view of the datepicker is inside a table that has .datepicker-days class .datepicker-daysdays视图位于具有.datepicker-days类的表中
    • the prev and next button are inside the .datepicker-days table and they have .prev and .next classes prev和next按钮位于.datepicker-days表中,并且具有.prev.next
    • the month name is in the same table and it has the .datepicker-switch class 月份名称在同一表中,并且具有.datepicker-switch
  • The date datepicker should show the right number of days, so you have to dinamically update defaultViewDate when year and month are selected (see changeDate event) datepicker日期应该显示正确的天数,因此当选择了年和月时,必须动态更新defaultViewDate (请参阅changeDate事件)
    • Since the picker has no setter method for defaultViewDate , you can destroy it and then re-init it. 由于选择器没有defaultViewDate setter方法,因此可以destroy它,然后重新初始化它。

Here a full working sample: 这里是一个完整的工作示例:

 $(document).ready(function() { var dateOpt = { format: 'dd', maxViewMode: 0 }; $("#year").datepicker({ format: "yyyy", viewMode: "years", minViewMode: "years" }).on('changeDate', function(e) { var month = $('#month').datepicker('getDate'); if( !month ){ month = new Date(); } dateOpt.defaultViewDate = { year: e.date.getFullYear(), month: month.getMonth(), day: 1 }; // Reset date datepicker $('#date').datepicker('destroy'); // Re-init with defaultViewDate option $('#date').datepicker(dateOpt); }); $("#month").datepicker({ format: "mm", viewMode: "months", minViewMode: "months", maxViewMode: 0 }).on('changeDate', function(e) { var year = $('#year').datepicker('getDate'); if( !year ){ year = new Date(); } dateOpt.defaultViewDate = { year: year.getFullYear(), month: e.date.getMonth(), day: 1 }; $('#date').datepicker('destroy'); $('#date').datepicker(dateOpt); }); $("#date").datepicker(dateOpt) }); 
 /* Hide prev/next buttons and month name*/ .datepicker-days>table>thead>tr>th.prev, .datepicker-days>table>thead>tr>th.datepicker-switch, .datepicker-days>table>thead>tr>th.next { display: none; } /* Hide days of previous month */ td.old.day{ visibility: hidden; } /* Hide days of next month */ td.new.day{ display: none; } 
 <link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet"/> <link href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.4/css/bootstrap-datepicker3.css" rel="stylesheet"/> <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.4/js/bootstrap-datepicker.js"></script> <div class="container"> <div class="form-group row"> <label class="form-control-label col-md-3" for="year">Year</label> <div class="col-md-9"> <input type="text" id="year" class="form-control"> </div> </div> <div class="form-group row"> <label class="form-control-label col-md-3" for="year">Month</label> <div class="col-md-9"> <input type="text" id="month" class="form-control"> </div> </div> <div class="form-group row"> <label class="form-control-label col-md-3" for="year">Date</label> <div class="col-md-9"> <input type="text" id="date" class="form-control"> </div> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM