Javascript JQuery 日期选择器设置默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10400934/
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
JQuery datepicker set defaultvalue
提问by xander27
I use this date picker. I need input to be empty on page loading, but set default value to today on datepicke popap. Now when I jast write:
我使用这个日期选择器。我需要在页面加载时输入为空,但在 datepicke popap 上将默认值设置为今天。现在,当我刚写:
$(".dp").datepicker({
format: 'dd.mm.yyyy',
startDate: '01.01.2012',
endDate: ''
});;
It's first Janary 1970 by default when datapicker is popuping. How can I change default value?
默认情况下,当数据选择器弹出时,它是 1970 年一月。如何更改默认值?
回答by T.J. Crowder
You can do it from the show
event, but I have to say that datepicker's documentation is really lacking. Here's how:
你可以从show
事件中做到这一点,但我不得不说 datepicker 的文档真的很缺乏。就是这样:
(function() {
$(".dp").datepicker({
format: 'dd.mm.yyyy',
startDate: '01.01.2012',
endDate: ''
}).on("show", function() {
$(this).val("01.05.2012").datepicker('update');
});
})();
Naturally, where I have 01.05.2012
, put the actual date.
当然,在我有的地方01.05.2012
,放上实际日期。
回答by Sebastian Castaldi
quick but works
快速但有效
var myDate = new Date();
var date = myDate.getFullYear() + '-' + ('0'+ myDate.getMonth()+1).slice(-2) + '-' + ('0'+ myDate.getDate()).slice(-2);
$("#datepicker").val(date);
回答by Charles Harmon
If anyone runs into this, where they want the default value in the input value to show as well as initialize datetimepicker, this works for me. I set the value in the html to data-val and check for that in the ready function. Then, I use moment to covert the string and set the initial picker value:
如果有人遇到这个问题,他们希望输入值中的默认值显示并初始化 datetimepicker,这对我有用。我将 html 中的值设置为 data-val 并在就绪函数中检查该值。然后,我使用 moment 来转换字符串并设置初始选择器值:
$(function(){
if($('#complianceBy').data('val').length){
$('#complianceBy').data('DateTimePicker').date(moment($('#complianceBy').data('val')));
}
});
回答by Stefan Vogt
In bootstrap-datepicker.js (~ Line 331) change:
在 bootstrap-datepicker.js(~第 331 行)中更改:
date = new Date(1971, 1, 1, 0, 0, 0),
to
到
date = new Date(),