Javascript 如何在 bootstrap datetimepicker 中放置默认日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41897021/
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
How to put a default date in bootstrap datetimepicker
提问by nethken
I want my datetimepickerto have a default value which is the current date. How can I achieve this? I already read the documentation but can't find anything to achieve that. Can someone help me? I want the format of the default value is MM dd, yyyy.
我希望我datetimepicker有一个默认值,即当前日期。我怎样才能做到这一点?我已经阅读了文档,但找不到任何东西来实现它。有人能帮我吗?我希望默认值的格式是MM dd, yyyy.
Here's my code.
这是我的代码。
<div class="form-group">
<label for="dtp_input2">Date</label>
<div class="input-group date form_date col-md-3" data-date="" data-date-format="MM dd, yyyy">
<input id="dtp_input2" name="date" class="form-control" size="10" type="text" value="" readonly>
<span class="input-group-addon">
<span class="glyphicon glyphicon-remove"></span>
</span>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
$('.form_date').datetimepicker({
weekStart: 1,
todayBtn: 1,
autoclose: 1,
todayHighlight: 1,
startView: 2,
minView: 2,
forceParse: 0,
});
采纳答案by Matt Spinks
Just set the value after setting up the datetimepicker:
只需在设置 datetimepicker 后设置值:
$('.form_date').datetimepicker({
weekStart: 1,
todayBtn: 1,
autoclose: 1,
todayHighlight: 1,
startView: 2,
minView: 2,
forceParse: 0,
});
$('#dtp_input2').val(Date());
回答by gustav
In my case :
Insert this defaultDate: new Date()code into your javascript datetimepicker.
就我而言:将此defaultDate: new Date()代码插入到您的 javascript datetimepicker 中。
<script type="text/javascript">
$(function () {
$('#datetimepicker10').datetimepicker({
defaultDate: new Date(),
viewMode: 'years',
format: 'MM/DD/YYYY'
});
});
回答by Daniel
Looks like from the documentationthat you can set a defaultDatewhen instantiating the datetimepicker.
从文档中看起来,您可以defaultDate在实例化日期时间选择器时设置 a 。
$(function () {
$('#datetimepicker5').datetimepicker({
defaultDate: "11/1/2013",
disabledDates: [
moment("12/25/2013"),
new Date(2013, 11 - 1, 21),
"11/22/2013 00:53"
]
});
});
回答by Gille Q.
This solution only uses the methods provided by the bootstrap datetimepicker.
此解决方案仅使用 bootstrap datetimepicker 提供的方法。
You can declare your datetimepicker as you did and then do this:
您可以像以前一样声明日期时间选择器,然后执行以下操作:
$('#datepicker input').datepicker('setDate', new Date());
$('#datepicker input').datepicker('update');
$('#datepicker input').val('');
This way the date you set in the setDate()will be selectionned and the input will remain empty.
If you don't want the input to be empty, just remove these lines:
这样,您在 中设置的日期setDate()将被选择并且输入将保持为空。如果您不希望输入为空,只需删除这些行:
$('#datepicker input').datepicker('update');
$('#datepicker input').val('');
And if you want to play with that solution: Here is a jsFiddle
如果您想使用该解决方案:这是一个jsFiddle
$('#datepicker input').datepicker({
weekStart: 1,
todayBtn: 1,
autoclose: 1,
todayHighlight: 1,
startView: 2,
minView: 2,
forceParse: 0,
});
$('#datepicker input').datepicker('setDate', new Date());
$('#datepicker input').datepicker('update');
$('#datepicker input').val('');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/js/bootstrap-datepicker.js"></script>
<link href="http://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/css/datepicker3.css" rel="stylesheet"/>
<div id="datepicker">
<input type="text" type="text" class="form-control" />
</div>
回答by Ambrish Pathak
You can also set current Date to a text field with JS like :
您还可以使用 JS 将当前日期设置为文本字段,例如:
var d = new Date();
document.getElementById("target").value = d.toDateString();
<input type="text" id="target">


