jQuery UI DatePicker - 更改日期格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1328025/
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
jQuery UI DatePicker - Change Date Format
提问by tarnfeld
I am using the UI DatePicker from jQuery UI as the stand alone picker. I have this code:
我使用来自 jQuery UI 的 UI DatePicker 作为独立的选择器。我有这个代码:
<div id="datepicker"></div>
And the following JS:
以及以下JS:
$('#datepicker').datepicker();
When I try to return the value with this code:
当我尝试使用此代码返回值时:
var date = $('#datepicker').datepicker('getDate');
I am returned this:
我被退回这个:
Tue Aug 25 2009 00:00:00 GMT+0100 (BST)
Which is totally the wrong format. Is there a way I can get it returned in the format DD-MM-YYYY
?
这是完全错误的格式。有没有办法让它以格式返回DD-MM-YYYY
?
回答by AvatarKava
Here's one specific for your code:
这是您的代码的一个特定内容:
var date = $('#datepicker').datepicker({ dateFormat: 'dd-mm-yy' }).val();
More general info available here:
此处提供更多一般信息:
回答by sampath
inside the jQuery script code just paste the code.
在 jQuery 脚本代码中只需粘贴代码即可。
$( ".selector" ).datepicker({ dateFormat: 'yy-mm-dd' });
this should work.
这应该有效。
回答by kcsoft
The getDate
method of datepicker returns a date type, not a string.
getDate
datepicker的方法返回一个日期类型,而不是一个字符串。
You need to format the returned value to a string using your date format.
Use datepicker's formatDate
function:
您需要使用日期格式将返回值格式化为字符串。使用日期选择器的formatDate
功能:
var dateTypeVar = $('#datepicker').datepicker('getDate');
$.datepicker.formatDate('dd-mm-yy', dateTypeVar);
The full list of format specifiers is available here.
格式说明符的完整列表可在此处获得。
回答by Nasirali
Here complete code for date picker with date format (yy/mm/dd).
这是日期格式(yy/mm/dd)的日期选择器的完整代码。
Copy link below and paste in head tag :
复制下面的链接并粘贴到 head 标签中:
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script type="text/javascript">
$(function() {
$("#datepicker").datepicker({ dateFormat: "yy-mm-dd" }).val()
});
</script>
Copy below code and paste between body tag :
复制以下代码并粘贴到 body 标签之间:
Date: <input type="text" id="datepicker" size="30"/>
If you would like two(2) input type text like Start Date
and End Date
then use this script and change date format.
如果您想要两(2)个输入类型文本Start Date
,End Date
然后使用此脚本并更改日期格式。
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script type="text/javascript">
$(function() {
$("#startdate").datepicker({ dateFormat: "dd-mm-yy" }).val()
$("#enddate").datepicker({ dateFormat: "dd-mm-yy" }).val()
});
</script>
Two input text like :
两个输入文本,如:
Start Date: <input type="text" id="startdate" size="30"/>
End Date: <input type="text" id="enddate" size="30"/>
回答by Salman A
getDate
function returns a JavaScript date. Use the following code to format this date:
getDate
函数返回一个 JavaScript 日期。使用以下代码格式化此日期:
var dateObject = $("#datepicker").datepicker("getDate");
var dateString = $.datepicker.formatDate("dd-mm-yy", dateObject);
It uses a utility function which is built into datepicker:
它使用内置于 datepicker 的实用函数:
$.datepicker.formatDate( format, date, settings )
- Format a date into a string value with a specified format.
$.datepicker.formatDate( format, date, settings )
- 将日期格式化为具有指定格式的字符串值。
The full list of format specifiers is available here.
格式说明符的完整列表可在此处获得。
回答by Salman A
The format for parsed and displayed dates. This attribute is one of the regionalisation attributes. For a full list of the possible formats see the formatDate function.
Code examples Initialize a datepicker with the dateFormat option specified.
$( ".selector" ).datepicker({ dateFormat: 'yy-mm-dd' });
Get or set the dateFormat option, after init.
//getter var dateFormat = $( ".selector" ).datepicker( "option", "dateFormat" ); //setter $( ".selector" ).datepicker( "option", "dateFormat", 'yy-mm-dd' );
解析和显示日期的格式。该属性是区域化属性之一。有关可能格式的完整列表,请参阅 formatDate 函数。
代码示例 使用指定的 dateFormat 选项初始化日期选择器。
$( ".selector" ).datepicker({ dateFormat: 'yy-mm-dd' });
在 init 之后获取或设置 dateFormat 选项。
//getter var dateFormat = $( ".selector" ).datepicker( "option", "dateFormat" ); //setter $( ".selector" ).datepicker( "option", "dateFormat", 'yy-mm-dd' );
回答by Catalin
$("#datepicker").datepicker("getDate")
returns a date object, not a string.
$("#datepicker").datepicker("getDate")
返回一个日期对象,而不是一个字符串。
var dateObject = $("#datepicker").datepicker("getDate"); // get the date object
var dateString = dateObject.getFullYear() + '-' + (dateObject.getMonth() + 1) + '-' + dateObject.getDate();// Y-n-j in php date() format
回答by Muddasir Abbas
<script type="text/javascript">
$(function() {
$( "#date" ).datepicker( {minDate: '0', dateFormat: 'yy-dd-mm' } );
});
</script>
回答by Ritesh d joshi
Try to use the
尝试使用
$( ".selector" ).datepicker({ dateFormat: 'dd/mm/yy' });
and there is lots of option available For the datepicker you can find it Here
并且有很多可用的选项对于日期选择器,您可以在这里找到它
http://api.jqueryui.com/datepicker/
This is the way we call the datepicker with the parameter
这是我们使用参数调用日期选择器的方式
$(function() {
$('.selector').datepicker({
dateFormat: 'dd/mm/yy',
changeMonth: true,
numberOfMonths: 1,
buttonImage: 'contact/calendar/calendar.gif',
buttonImageOnly: true,
onSelect: function(selectedDate) {
// we can write code here
}
});
});
回答by Rohit
you can simply use this format in you function just like
你可以简单地在你的功能中使用这种格式,就像
$(function() {
$( "#datepicker" ).datepicker({
dateFormat: 'dd/mm/yy',
changeMonth: true,
changeYear: true
});
});
<input type="text" id="datepicker"></p>