jQuery UI DatePicker - Change Date Format

1,642,238

Solution 1

Here's one specific for your code:

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

More general info available here:

Solution 2

inside the jQuery script code just paste the code.

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

this should work.

Solution 3

The getDate method of datepicker returns a date type, not a string.

You need to format the returned value to a string using your date format. Use datepicker's formatDate function:

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

The full list of format specifiers is available here.

Solution 4

Here complete code for date picker with date format (yy/mm/dd).

Copy link below and paste in head tag :

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

      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.

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

Solution 5

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' );
Share:
1,642,238
tarnfeld
Author by

tarnfeld

Updated on December 20, 2021

Comments

  • tarnfeld
    tarnfeld over 2 years

    I am using the UI DatePicker from jQuery UI as the stand alone picker. I have this code:

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

    And the following 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?

    • Hein du Plessis
      Hein du Plessis over 6 years
      Perfect, thanks, why doesn't $.datepicker.formatDate( "yy-mm-dd", new Date( 2007, 1 - 1, 26 ) ); work as in the documenation?
  • Dave Jarvis
    Dave Jarvis almost 13 years
    .selector and #datepicker are not the same. Pasting your code verbatim would not work.
  • Ravi Y
    Ravi Y over 11 years
    How does this answer the original question about date formats?
  • bluish
    bluish over 11 years
    It would have been better if you enriched @kcsoft answer (like I'm gonna do) instead of posting a new one ;)
  • Ryan
    Ryan over 10 years
    The first line of code in this example makes the "Date Picker" work with a specific date format, rather than the default American mm/dd/yy format.
  • knocte
    knocte almost 10 years
    @Ryan it doesn't work for me, do you have an example which includes all lines of code?
  • Tommy
    Tommy over 9 years
    This just gives me the same format as I specified in the datepicker and not dd-mm-yy. using version 1.10. The answear below works fine.
  • SearchForKnowledge
    SearchForKnowledge over 9 years
    the yy is giving me 2015. How can I only get 15?
  • Shadoath
    Shadoath about 9 years
    @SearchForKnowledge just use y. datepicker-formatting
  • Sunil Kumar
    Sunil Kumar over 7 years
  • João Pimentel Ferreira
    João Pimentel Ferreira almost 7 years
    pay attention that if you already previously defined a different format, this doesn't work, you should do then $("#datepicker").datepicker("option", "dateFormat", "dd-mm-yy" ).val()
  • TarangP
    TarangP over 5 years
    in older versions format: 'dd-mm-yyyy', should work