我使用的UI DatePicker从jQuery UI作为独立的选择器。我有这样的代码:

<div id="datepicker"></div>

和下面的JS:

$('#datepicker').datepicker();

当我尝试用下面的代码返回值时:

var date = $('#datepicker').datepicker('getDate');

我得到的答复是:

Tue Aug 25 2009 00:00:00 GMT+0100 (BST)

这是完全错误的格式。有没有办法让它以DD-MM-YYYY格式返回?


当前回答

dateFormat 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' );

其他回答

这也是可行的:

$("#datepicker").datepicker({
    dateFormat:'d-m-yy'
});

dateFormat 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' );

你应该使用data-date-format="yyyy-mm-dd"在你的输入字段html和初始数据选择器在JQuery就像简单

$("#issue_date").datepicker({ 
    dateFormat: "yy-mm-dd",
    changeMonth: true,
    changeYear: true
});

如果你需要年月日格式的日期,你可以使用这个:-

$("#datepicker").datepicker({ dateFormat: "yy-mm-dd" });

您可以找到所有受支持的格式 https://jqueryui.com/datepicker/#date-formats

我在2015年12月6日需要,我这样做了:-

$("#datepicker").datepicker({ dateFormat: "d M,y" });
$("#datepicker").datepicker({ dateFormat: "yy-mm-dd" }).val()