Javascript 如何从 jQuery UI datepicker 获取日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4919873/
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 get the date from jQuery UI datepicker
提问by Vivek
I want to get the date from datepicker whenever user choose the date in jQuery UI datepicker and click the button on the form.
每当用户在 jQuery UI datepicker 中选择日期并单击表单上的按钮时,我都想从 datepicker 获取日期。
well I need to get the day, month and year of the date they choose. How can I get the date form jQuery UI?
好吧,我需要获得他们选择的日期的日、月和年。如何获取日期表单 jQuery UI?
回答by CoolEsh
Use
用
var jsDate = $('#your_datepicker_id').datepicker('getDate');
if (jsDate !== null) { // if any date selected in datepicker
jsDate instanceof Date; // -> true
jsDate.getDate();
jsDate.getMonth();
jsDate.getFullYear();
}
回答by Vivek
You can retrieve the date by using the getDate function:
您可以使用 getDate 函数检索日期:
$("#datepicker").datepicker( 'getDate' );
The value is returned as a JavaScript Date object.
该值作为 JavaScript 日期对象返回。
If you want to use this value when the user selects a date, you can use the onSelect event:
如果要在用户选择日期时使用此值,可以使用 onSelect 事件:
$("#datepicker").datepicker({
onSelect: function(dateText, inst) {
var dateAsString = dateText; //the first parameter of this function
var dateAsObject = $(this).datepicker( 'getDate' ); //the getDate method
}
});
The first parameter is in this case the selected Date as String. Use parseDate to convert it to a JS Date Object.
在这种情况下,第一个参数是所选日期作为字符串。使用 parseDate 将其转换为 JS 日期对象。
See http://docs.jquery.com/UI/Datepickerfor the full jQuery UI DatePicker reference.
有关完整的 jQuery UI DatePicker 参考,请参阅http://docs.jquery.com/UI/Datepicker。
回答by NameNotFoundException
Try this, works like charm, gives the date you have selected. onsubmit form try to get this value:-
试试这个,就像魅力一样,给出你选择的日期。onsubmit 表单尝试获取此值:-
var date = $("#scheduleDate").datepicker({ dateFormat: 'dd,MM,yyyy' }).val();
回答by alexl
the link to getdate: https://api.jqueryui.com/datepicker/#method-getDate
getdate 的链接:https: //api.jqueryui.com/datepicker/#method-getDate
$("#datepicker").datepicker( 'getDate' );
回答by realmag777
Sometimes a lot of troubles with it. In attribute value of datapicker data is 28-06-2014, but datepicker is show or today or nothing. I decided it in a such way:
有时会遇到很多麻烦。在 datapicker 数据的属性值为 28-06-2014,但 datepicker 是 show 或 today 或没有。我是这样决定的:
<input type="text" class="form-control datepicker" data-value="<?= date('d-m-Y', (!$event->date ? time() : $event->date)) ?>" value="<?= date('d-m-Y', (!$event->date ? time() : $event->date)) ?>" />
I added to the input of datapicker attribute data-value, because if call jQuery(this).val()OR jQuery(this).attr('value')- nothing works. I decided init in cycle each datapicker and take its value from attribute data-value:
我添加到 datapicker 属性数据值的输入,因为如果调用jQuery(this).val()OR jQuery(this).attr('value')- 没有任何效果。我决定循环初始化每个数据选择器并从属性数据值中获取其值:
$("form .datepicker").each(function() {
var time = jQuery(this).data('value');
time = time.split('-');
$(this).datepicker('setDate', new Date(time[2], time[1], time[0], 0, 0, 0));
});
and it is works fine =)
它工作正常 =)
回答by evobe
NamingException's answer worked for me. Except I used
NamingException 的答案对我有用。除了我用
var date = $("#date").dtpicker({ dateFormat: 'dd,MM,yyyy' }).val()
var date = $("#date").dtpicker({ dateFormat: 'dd,MM,yyyy' }).val()
datepicker
didn't work but dtpicker
did.
datepicker
没有工作,但dtpicker
做了。