在 JavaScript 中获取 bootstrap Datetimepicker 的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16573624/
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
Get the value of bootstrap Datetimepicker in JavaScript
提问by dxtr
I need to get the value of Datetimepicker in my JavaScript function. I have made something like this, but it doesn't work:
我需要在我的 JavaScript 函数中获取 Datetimepicker 的值。我做了这样的事情,但它不起作用:
$("#date").click( function(){
alert(document.getElementById('datetimepicker1').value);
});
It gives me 'undefined'
它给了我“未定义”
回答by Ian
Either use:
要么使用:
$("#datetimepicker1").data("datetimepicker").getDate();
Or (from looking at the page source):
或者(从查看页面源代码):
$("#datetimepicker1").find("input").val();
The returned value will be a Date
(for the first example above), so you need to format it yourself:
返回值将是 a Date
(对于上面的第一个示例),因此您需要自己对其进行格式化:
var date = $("#datetimepicker1").data("datetimepicker").getDate(),
formatted = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + " " + date.getHours + ":" + date.getMinutes() + ":" + date.getSeconds();
alert(formatted);
Also, you could just set the format as an attribute:
此外,您可以将格式设置为属性:
<div id="datetimepicker1" class="date">
<input data-format="yyyy-MM-dd hh:mm:ss" type="text"></input>
</div>
and you could use the $("#datetimepicker1").find("input").val();
你可以使用 $("#datetimepicker1").find("input").val();
回答by Alexandre Bourlier
It seems the doc evolved.
似乎文档进化了。
One should now use :
$("#datetimepicker1").data("DateTimePicker").date()
.
现在应该使用 :
$("#datetimepicker1").data("DateTimePicker").date()
。
NB: Doing so return a Moment object, not a Date object
注意:这样做会返回一个Moment 对象,而不是一个 Date 对象
回答by Giang Phan
To call the Bootstrap-datetimepikcer supported functions, you should use the syntax:
要调用 Bootstrap-datetimepikcer 支持的函数,您应该使用以下语法:
$('#datetimepicker').data("DateTimePicker").FUNCTION()
So you can try the function:
所以你可以试试这个功能:
$('#datetimepicker').data("DateTimePicker").date();
Documentation: http://eonasdan.github.io/bootstrap-datetimepicker/Functions/
文档:http: //eonasdan.github.io/bootstrap-datetimepicker/Functions/
回答by bds
Or try $("#datetimepicker").data().date;
或者试试 $("#datetimepicker").data().date;
回答by Andreas Bigger
Since the return value has changed, $("#datetimepicker1").data("DateTimePicker").date()
actually returns a moment object as Alexandre Bourlier stated:
由于返回值发生了变化,$("#datetimepicker1").data("DateTimePicker").date()
实际上返回了一个矩对象,正如 Alexandre Bourlier 所说:
It seems the doc evolved.
One should now use : $("#datetimepicker1").data("DateTimePicker").date().
NB : Doing so return a Moment object, not a Date object
似乎文档进化了。
现在应该使用:$("#datetimepicker1").data("DateTimePicker").date()。
注意:这样做会返回一个 Moment 对象,而不是一个 Date 对象
Therefore, we must use .toDate() to change this statement to a date as such:
因此,我们必须使用 .toDate() 将此语句更改为这样的日期:
$("#datetimepicker1").data("DateTimePicker").date().toDate();
$("#datetimepicker1").data("DateTimePicker").date().toDate();
回答by user3612888
I'm using the latest Bootstrap 3 DateTime Picker (http://eonasdan.github.io/bootstrap-datetimepicker/)
我正在使用最新的 Bootstrap 3 DateTime Picker ( http://eonasdan.github.io/bootstrap-datetimepicker/)
This is how you should use DateTime Picker inline:
这是您应该如何内联使用 DateTime Picker:
var selectedDate = $("#datetimepicker").find(".active").data("day");
The above returned: 03/23/2017
以上返回:03/23/2017
回答by uomopalese
This is working for me using this Bootsrap Datetimepiker, it returns the value as it is shown in the datepicker input, e.g. 2019-04-11
这对我来说使用这个Bootsrap Datetimepiker,它返回日期选择器输入中显示的值,例如2019-04-11
$('#myDateTimePicker').on('click,focusout', function (){
var myDate = $("#myDateTimePicker").val();
//console.log(myDate);
//alert(myDate);
});
回答by Giorgi Grdzelidze
Try this (worked for me), when i had boostrap inline date time picker this is a way how i get selected datetime.
试试这个(对我有用),当我有 boostrap 内联日期时间选择器时,这是我选择日期时间的一种方式。
var selected_datetime = $("#datetimepicker").data("date");
回答by weBBer
I tried all the above methods and I did not get the value properly in the same format
, then I found this.
我尝试了上述所有方法,但没有在同一个中正确获取值format
,然后我找到了这个。
$("#datetimepicker1").find("input")[1].value;
The above code will return the value in the same format as in the datetime picker.
上面的代码将以与日期时间选择器中相同的格式返回值。
This may help you guys in the future.
这可能会在未来对你们有所帮助。
Hope this was helpful..
希望这有帮助..