我使用的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格式返回?


当前回答

这也是可行的:

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

其他回答

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

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

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

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

$("#datepicker").datepicker({ dateFormat: "d M,y" });

这里有一个特定的代码:

var date = $('#datepicker').datepicker({ dateFormat: 'dd-mm-yy' }).val();

更多信息可在这里:

http://api.jqueryui.com/datepicker/#option-dateFormat http://api.jqueryui.com/datepicker/#utility-formatDate

<script type="text/javascript">
  $(function() {
    $( "#date" ).datepicker( {minDate: '0', dateFormat: 'yy-dd-mm' } );
  });
</script>

所以我纠结于此,认为我疯了,事情是bootstrap datepicker被实现而不是jQuery,所以选项是不同的;

$('.dateInput').datepicker({  format: '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' );