1.控件下載地址:http://www.bootcss.com/p/bootstrap-datetimepicker/index.htm,參數設置說明也在這個鏈接下面:
2.具體參數說明(復制原鏈接):
format(設置時間格式、詳細介紹見原鏈接):
- yyyy-mm-dd
- yyyy-mm-dd hh:ii
- yyyy-mm-ddThh:ii
- yyyy-mm-dd hh:ii:ss
- yyyy-mm-ddThh:ii:ssZ
weekStart:
Integer. 默認值:0
一周從哪一天開始。0(星期日)到6(星期六)
startDate:
Date. 默認值:開始時間
endDate
Date. 默認值:結束時間
The latest date that may be selected; all later dates will be disabled.
daysOfWeekDisabled
String, Array. 默認值: '', []
Days of the week that should be disabled. Values are 0 (Sunday) to 6 (Saturday). Multiple values should be comma-separated. Example: disable weekends: '0,6' or [0,6].
autoclose
Boolean. 默認值:false
當選擇一個日期之后是否立即關閉此日期時間選擇器。
startView
Number, String. 默認值:2, 'month'
日期時間選擇器打開之后首先顯示的視圖。 可接受的值:
- 0 or 'hour' for the hour view
- 1 or 'day' for the day view
- 2 or 'month' for month view (the default)
- 3 or 'year' for the 12-month overview
- 4 or 'decade' for the 10-year overview. Useful for date-of-birth datetimepickers.
minView
Number, String. 默認值:0, 'hour'
日期時間選擇器所能夠提供的最精確的時間選擇視圖。
maxView
Number, String. 默認值:4, 'decade'
日期時間選擇器最高能展示的選擇范圍視圖。
todayBtn
Boolean, "linked". 默認值: false
如果此值為true 或 "linked",則在日期時間選擇器組件的底部顯示一個 "Today" 按鈕用以選擇當前日期。如果是true的話,"Today" 按鈕僅僅將視圖轉到當天的日期,如果是"linked",當天日期將會被選中。
todayHighlight
Boolean. 默認值: false
如果為true, 高亮當前日期。
keyboardNavigation
Boolean. 默認值: true
是否允許通過方向鍵改變日期。
language
String. 默認值: 'en'
The two-letter code of the language to use for month and day names. These will also be used as the input's value (and subsequently sent to the server in the case of form submissions). Currently ships with English ('en'), German ('de'), Brazilian ('br'), and Spanish ('es') translations, but others can be added (see I18N below). If an unknown language code is given, English will be used.
forceParse
Boolean. 默認值: true
當選擇器關閉的時候,是否強制解析輸入框中的值。也就是說,當用戶在輸入框中輸入了不正確的日期,選擇器將會盡量解析輸入的值,並將解析后的正確值按照給定的格式format設置到輸入框中。
minuteStep
Number. 默認值: 5
此數值被當做步進值用於構建小時視圖。對於每個 minuteStep 都會生成一組預設時間(分鍾)用於選擇。
pickerReferer : 不建議使用
String. 默認值: 'default' (other value available : 'input')
The referer element to place the picker for the component implementation. If you want to place the picker just under the input field, just specify input.
pickerPosition
String. 默認值: 'bottom-right' (還支持 : 'bottom-left')
此選項當前只在組件實現中提供支持。通過設置選項可以講選擇器放倒輸入框下方。
viewSelect
Number or String. 默認值: same as minView (supported values are: 'decade', 'year', 'month', 'day', 'hour')
With this option you can select the view from which the date will be selected. By default it's the last one, however you can choose the first one, so at each click the date will be updated.
showMeridian
Boolean. 默認值: false
This option will enable meridian views for day and hour views.
initialDate
Date or String. 默認值: new Date()
You can initialize the viewer with a date. By default it's now, so you can specify yesterday or today at midnight
標記:
組件模版:
<div class="input-append date" id="datetimepicker" data-date="12-02-2012" data-date-format="dd-mm-yyyy"> <input class="span2" size="16" type="text" value="12-02-2012"> <span class="add-on"><i class="icon-th"></i></span> </div>
帶有重置按鈕(用於清空輸入框)的組件模版:
<div class="input-append date" id="datetimepicker" data-date="12-02-2012" data-date-format="dd-mm-yyyy"> <input class="span2" size="16" type="text" value="12-02-2012"> <span class="add-on"><i class="icon-remove"></i></span> <span class="add-on"><i class="icon-th"></i></span> </div>
方法
.datetimepicker(options)
初始化日期時間選擇器。
remove
參數: None
移除日期時間選擇器。同時移除已經綁定的event、內部綁定的對象和HTML元素。
$('#datetimepicker').datetimepicker('remove');
show
參數: None
顯示日期時間選擇器。
$('#datetimepicker').datetimepicker('show');
hide
隱藏日期時間選擇器。
$('#datetimepicker').datetimepicker('hide');
update
參數: None
使用當前輸入框中的值更新日期時間選擇器。
$('#datetimepicker').datetimepicker('update');
setEndDate
參數:
- endDate (String)
給日期時間選擇器設置結束日期。
$('#datetimepicker').datetimepicker('setEndDate', '2012-01-01');
Omit endDate (or provide an otherwise falsey value) to unset the limit.
$('#datetimepicker').datetimepicker('setEndDate'); $('#datetimepicker').datetimepicker('setEndDate', null);
setDaysOfWeekDisabled
參數:
- daysOfWeekDisabled (String|Array)
Sets the days of week that should be disabled.
$('#datetimepicker').datetimepicker('setDaysOfWeekDisabled', [0,6]);
Omit daysOfWeekDisabled (or provide an otherwise falsey value) to unset the disabled days.
$('#datetimepicker').datetimepicker('setDaysOfWeekDisabled'); $('#datetimepicker').datetimepicker('setDaysOfWeekDisabled', null);
事件(Events)
Datetimepicker 類暴露了一組event用以對日期進行操作。
show
當選擇器顯示時被觸發。
hide
當選擇器隱藏時被觸發。
changeDate
當日期被改變時被觸發。
$('#date-end') .datetimepicker() .on('changeDate', function(ev){ if (ev.date.valueOf() < date-start-display.valueOf()){ .... } });
changeYear
當十年視圖上的年視圖view被改變時觸發。
changeMonth
當年視圖上的月視圖view被改變時觸發。
outOfRange
當用戶選擇的日期超出startDate 或endDate 時,或者通過setDate 或 setUTCDate方法設置日期超出范圍時被觸發。
鍵盤支持
日期時間選擇器提供了鍵盤導航:
up, down, left, right 方向鍵
這些方向鍵中,left/right 向后/向前 一天,up/down 向后/向前 一周。
配合shift鍵,up/left 向后退一個月,down/right 向前進一個月。
配置ctrl鍵,up/left 向后退一年,down/right 向前進一年。
Shift+ctrl 和 ctrl 同等效果 - 也就是說,他們不能同時改變月和年,只能單獨改變年。
escape
escape 鍵可以用來隱藏、重新顯示日期時間選擇器;當用戶希望手工編輯輸入框中的值是會很有用。
enter
當選擇器處於顯示狀態時,enter鍵只是將其隱藏。當選擇器處於隱藏狀態時,enter鍵發揮通常的功能 - 提交當前表單,或者其他。
I18N國際化
本插件支持月、每周中天的名稱、weekStart選項的國際化。默認是語言是English ('en');其它可以使用的翻譯文件在js/locales/ 目錄中,只需在本插件之后引入需要的語言文件即可。需要增加額外語言支持的話,只需向 $.fn.datetimepicker.dates中增加一個key即可,一定要放在調用 .datetimepicker()之前。如下案例:
$.fn.datetimepicker.dates['en'] = { days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"], daysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"], daysMin: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"], months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"], monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"], today: "Today" };
Right-to-left languages may also include rtl: true to make the calendar display appropriately.
If your browser (or those of your users) is displaying characters wrong, chances are the browser is loading the javascript file with a non-unicode encoding. Simply add charset="UTF-8" to your script tag:
<script type="text/javascript" src="bootstrap-datetimepicker.de.js" charset="UTF-8"></script>
使用
綁定輸入框,並設置format選項:
<input type="text" value="2012-05-15 21:05" id="datetimepicker"> $('#datetimepicker').datetimepicker({ format: 'yyyy-mm-dd hh:ii' });
綁定輸入框,並設置format標記:
<input type="text" value="2012-05-15 21:05" id="datetimepicker" data-date-format="yyyy-mm-dd hh:ii"> $('#datetimepicker').datetimepicker();
作為組件使用:
<div class="input-append date" id="datetimepicker" data-date="12-02-2012" data-date-format="dd-mm-yyyy"> <input size="16" type="text" value="12-02-2012" readonly> <span class="add-on"><i class="icon-th"></i></span> </div> $('#datetimepicker').datetimepicker();
作為內聯日期時間選擇器:
<div id="datetimepicker"></div>
$('#datetimepicker').datetimepicker();
3.
1)添加秒顯示下拉框,查找bootstrap-datetimepicker.js,找到this.picker.find('.datetimepicker-minutes td').html(html.join(''))這句,大概在709行附近,改為下面的
//添加datetimepicker下拉框,如果配置要選擇秒,否則不添加 var addSec = /,s{1,2},/.test(',' + this.format.parts.join(',') + ','), sSec = addSec ? '<select style="width:100%">' : ''; this.addSec = addSec; if (addSec) { var orgSec = this.viewDate.getSeconds(); for (var _i = 0; _i < 60; _i++) sSec += '<option value="' + _i + '"' + (_i == orgSec ? ' selected' : '') + '>' + (_i < 10 ? '0' : '') + _i + '</option>'; sSec += '</select>'; } this.picker.find('.datetimepicker-minutes td').html(html.join('') + sSec);
2)修改容器點擊事件。大概846行,增加下面的紅色語句,防止選擇select秒鍾時日期彈出框隱藏
click: function (e) { e.stopPropagation(); e.preventDefault(); ///防止選擇秒鍾選擇器日期控件層隱藏 if (e.target.tagName == 'SELECT' || e.target.tagName == 'OPTION') return;
3)然后繼續往下找到case 'span'語句,大概916,修改里面秒鍾獲取方式,如下所示
case 'span': if (!target.is('.disabled')) { var year = this.viewDate.getUTCFullYear(), month = this.viewDate.getUTCMonth(), day = this.viewDate.getUTCDate(), hours = this.viewDate.getUTCHours(), minutes = this.viewDate.getUTCMinutes(), //修改datetimepicker秒鍾獲取方式 seconds = this.addSec ? this.picker.find('select').val() : this.viewDate.getUTCSeconds();
到此完成bootstrap datetimepicker添加秒鍾選擇下拉框功能。點擊這里下載修改過的bootstrap-datetimepicker.js文件,注意:一定要配置format顯示秒鍾才會顯示秒鍾下拉框,否則不顯示
$('.form_datetime').datetimepicker({ weekStart: 1, todayBtn: 1, autoclose: 1, todayHighlight: 1, startView: 2, forceParse: 0, showMeridian: 1, format: 'yyyy-mm-dd hh:ii:ss'/// });
原文鏈接:http://www.bootcss.com/p/bootstrap-datetimepicker/index.htm
bootStrap-datetimepicker在火狐瀏覽器下報錯的問題:
問題:使用bootstrap-datetimepicker這個日期插件來顯示日期,但在火狐下報如下錯誤:TypeError: (intermediate value).toString(...).split(...)[1] is undefined。
解決方案:將插件中的this.defaultTimeZone=(new Date).toString().split("(")[1].slice(0,-1);改為this.defaultTimeZone='GMT '+(new Date()).getTimezoneOffset()/60;
