get selected value in datePicker and format it

夙愿已清 提交于 2020-01-01 03:26:06

问题


In the datePicker selected 20.04.2012. I want to get this value, format it and store in a variable. I uses this code:

var date = $("#scheduleDate").datepicker({ dateFormat: 'dd,MM,yyyy' }).val();

But result is 20.04.2012. Also, I tryed this code:

var dateTypeVar = $('#scheduleDate').datepicker('getDate');
$.datepicker.formatDate('dd-MM-yy', dateTypeVar);

Result is Fri Apr 20 2012 00:00:00 GMT+0600.

I want to get the following: 20, April, 2012
How to format properly?
Thanks.


回答1:


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

dateString = $.datepicker.formatDate('dd, MM, yy', new Date("20 April 2012"));

I've updated the jsfiddle for experimenting with this




回答2:


var dateObject = $("#datePickerInput").datepicker('getDate');
$.datepicker.formatDate('dd MM, yy', dateObject);



回答3:


Use jquery-dateFormat. It will solve your problem.

You need to include the jquery.dateFormat in your html file.

<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>



回答4:


$('#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



来源:https://stackoverflow.com/questions/10266456/get-selected-value-in-datepicker-and-format-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!