jQuery 在 datePicker 中获取选定的值并对其进行格式化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10266456/
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 selected value in datePicker and format it
提问by user348173
In the datePicker selected 20.04.2012
. I want to get this value, format it and store in a variable.
I uses this code:
在 datePicker 中选中20.04.2012
。我想获取这个值,格式化它并存储在一个变量中。我使用这个代码:
var date = $("#scheduleDate").datepicker({ dateFormat: 'dd,MM,yyyy' }).val();
But result is 20.04.2012
.
Also, I tryed this code:
但结果是20.04.2012
。另外,我尝试了以下代码:
var dateTypeVar = $('#scheduleDate').datepicker('getDate');
$.datepicker.formatDate('dd-MM-yy', dateTypeVar);
Result is Fri Apr 20 2012 00:00:00 GMT+0600
.
结果是Fri Apr 20 2012 00:00:00 GMT+0600
。
I want to get the following: 20, April, 2012
How to format properly?
Thanks.
我想获得以下信息:20, April, 2012
如何正确格式化?
谢谢。
回答by drinchev
If you want to take the formatted value of input do this :
如果要采用输入的格式化值,请执行以下操作:
$("input").datepicker({ dateFormat: 'dd, mm, yy' });
later in your code when the date is set you could get it by
稍后在您的代码中设置日期时,您可以通过
dateVariable = $("input").val();
If you want just to take a formatted value with datepicker you might want to use the utility
如果您只想使用 datepicker 获取格式化的值,您可能需要使用该实用程序
dateString = $.datepicker.formatDate('dd, MM, yy', new Date("20 April 2012"));
I've updated the jsfiddlefor experimenting with this
我已经更新了jsfiddle来试验这个
回答by pritam kumar
var dateObject = $("#datePickerInput").datepicker('getDate');
$.datepicker.formatDate('dd MM, yy', dateObject);
回答by sarwar026
Use jquery-dateFormat. It will solve your problem.
使用jquery-dateFormat。它会解决你的问题。
You need to include the jquery.dateFormat in your html file.
您需要在 html 文件中包含 jquery.dateFormat。
<script>
var date = $('#scheduleDate').val();
document.write($.format.date(date, "dd,MM,yyyy"));
var dateTypeVar = $('#scheduleDate').datepicker('getDate');
document.write($.format.date(dateTypeVar, "dd-MM-yy"));
</script>
回答by mr.java
$('#scheduleDate').datepicker({ dateFormat : 'dd, MM, yy'});
var dateFormat = $('#scheduleDate').datepicker('option', 'dd, MM, yy');
$('#scheduleDate').datepicker('option', 'dateFormat', 'dd, MM, yy');
var result = $('#scheduleDate').val();
alert('result: ' + result);
result: 20, April, 2012
结果:2012 年 4 月 20 日