twitter-bootstrap Bootstrap Datetimepicker 设置日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40366878/
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
Bootstrap Datetimepicker set date
提问by JRsz
I am using a datetimepickerfrom Eonasdan which works fine so far.
我正在使用Eonasdan的日期时间选择器,到目前为止效果很好。
I have a HTML element like this
我有一个像这样的 HTML 元素
<div id="datetimepicker"></div>
and use the datetimepicker function like this:
并像这样使用 datetimepicker 函数:
$("#datetimepicker").datetimepicker({
inline: true,
sideBySide: true,
stepping: 5,
locale: "de"
});
I want to update the date via Javascript using the datefunction. My try looks like this:
我想使用date函数通过 Javascript 更新日期。我的尝试看起来像这样:
$("#datetimepicker").data("DateTimePicker").date("2016-01-01");
Unfortunately this only returns a large object and does not alter the visible date in any way. This object is returned
不幸的是,这只会返回一个大对象并且不会以任何方式改变可见日期。返回此对象
Object { destroy: c/l.destroy(), toggle: c/ha(), show: c/ga(), hide: c/ba(),
disable: c/l.disable(), enable: c/l.enable(), ignoreReadonly: c/l.ignoreReadonly(),
options: c/l.options(), date: c/l.date(), format: c/l.format(), 40 weitere… }
Using a different time format or taking only date and no time or whatsoever does not help either.
使用不同的时间格式或只取日期而没有时间或任何其他方式也无济于事。
None of the suggested solutions I found on various websites have solved my problem. I don't know whether I have to update the element? If so, how do I do it? I could not find any hint in the docs...
我在各种网站上找到的建议解决方案都没有解决我的问题。不知道要不要更新元素?如果是这样,我该怎么做?我在文档中找不到任何提示...
It is not an option to use the defaultDate since I need to do this process twice with two different dates.
使用 defaultDate 不是一个选项,因为我需要用两个不同的日期执行此过程两次。
回答by Alexanderp
As per documentationyou can use string, Date, moment, null, string and Date examples are below. Note default picker format.
根据您可以使用的文档string, Date, moment, null,字符串和日期示例如下。注意默认选择器格式。
$('#datetimepicker1').data("DateTimePicker").date(new Date())
or
或者
$('#datetimepicker1').data("DateTimePicker").date('1/11/2016 12:23:12')
回答by Ortsbo
I think your HTML element should be an Input tag:
我认为你的 HTML 元素应该是一个 Input 标签:
<input id="datetimepicker" type="text">
When the document ready:
当文件准备好时:
$(document).ready(function () {
$('#datetimepicker').datetimepicker({
defaultDate: null,
format: 'LT'
});
})
You can set your date value via JavaScript.
您可以通过 JavaScript 设置日期值。
$('#datetimepicker').data("DateTimePicker").date(new Date())
$('#datetimepicker').data("DateTimePicker").date(new Date())
or set null value, which is showing empty value.
或设置空值,即显示空值。
$('#datetimepicker').data("DateTimePicker").date(null)
$('#datetimepicker').data("DateTimePicker").date(null)
I tested it. It's working.
我测试了它。它正在工作。
回答by ivan
Sure, i'm real Necromancer, but i got same problem and .date() function does not help for me. But, i found this:
当然,我是真正的死灵法师,但我遇到了同样的问题并且 .date() 函数对我没有帮助。但是,我发现了这个:
$('#datetimepicker1').data("DateTimePicker").options({ defaultDate: newdate});
// newdate - could be Date, Moment or ISO string
回答by Umashankar
I have debugged a little bit and I found this solution and it's working for me.
我已经调试了一点,我找到了这个解决方案,它对我有用。
$('#datetimepicker').data("DateTimePicker").setValue('2020-05-3')
You will get all functions of datepicker by consoling this $('#datetimepicker').data("DateTimePicker")
您将通过安慰此获得 datepicker 的所有功能 $('#datetimepicker').data("DateTimePicker")


