Why can't changing dateFormat of my jQuery datepicker fail?

I know there are many questions here about changing datepicker dateFormat, but I follow the instructions exactly and they seem to have no effect. Here is the exact code that was suggested for the other answers:

$(document).ready(function() { $('#datepicker').datepicker({ dateFormat: 'dd-mm-yy' }); }); 

However, when I run it and check the datepicker value with alert($('#datepicker').datepicker("getDate"));

I am returning the date in an undesirable format! This is very confusing. Am I something wrong? See the described behavior http://jsfiddle.net/TmdM8/

edit: even more confusing is that if I read the date format using alert($("#datepicker").datepicker('option', 'dateFormat'));

... the returned dateFormat is true even if the getDate call does not return the date in the desired format. See http://jsfiddle.net/fWmBe/

+4
source share
4 answers

The date format option is for the format that appears when it appears in the text box to which the collector applies.

see my updated version of your violin:

http://jsfiddle.net/TmdM8/1/

as you can see, when you pcick in the text box and select the date, it is in the format you specified.
as far as I know, the getDate function returns a javascript date.

EDIT: if you want to format the output from javascript date, you will need to use a custom script or library like date.js (I have had great success with this).

http://code.google.com/p/datejs/

so you can do something like:

 $('#datepicker').datepicker("getDate").toString('dd-MM-yyyy'); 

to get the date in the format you like

+5
source

I agree that it returns a javascript date. You can use this utility function:

$ datepicker.formatDate (format, date, settings) - formatting the date to a string value with the specified format.

from http://docs.jquery.com/UI/Datepicker/formatDate

0
source

What happens here is getDate - this is returning a javascript date object. You see the date toString object.

Try the following:

 $(document).ready(function() { $('#datepicker').datepicker({ onSelect: function(dateText, inst) { alert(dateText) } }); $('#datepicker').datepicker( "option" , 'dateFormat', 'yyyy-mm-dd' ) }); 
0
source

in case someone is looking for an answer like me. here is the working code:

 <script> $(function() { $( "#datepicker" ).datepicker({inline: true, dateFormat:'yy-mm-dd'}); }); </script> 
0
source

All Articles