jQuery UI DatePicker - Change Date Format

前端 未结 28 1837
后悔当初
后悔当初 2020-11-21 16:39

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

And the

相关标签:
28条回答
  • 2020-11-21 17:03

    Just run this HTML page, You can see several formats.

    <html lang="en">
    <head>
    <meta charset="utf-8" />
    <title>jQuery UI Datepicker - Format date</title>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    <link rel="stylesheet" href="/resources/demos/style.css" />
    <script>
    $(function() {
        $( "#datepicker" ).datepicker();
        $( "#format" ).change(function() {
            $( "#datepicker" ).datepicker( "option", "dateFormat", $( this ).val() );
        });
    });
    </script>
    </head>
    <body>
    
    <p>Date: <input type="text" id="datepicker" size="30" /></p>
    
    <p>Format options:<br />
    <select id="format">
        <option value="mm/dd/yy">Default - mm/dd/yy</option>
        <option value="yy-mm-dd">ISO 8601 - yy-mm-dd</option>
        <option value="d M, y">Short - d M, y</option>
        <option value="d MM, y">Medium - d MM, y</option>
        <option value="DD, d MM, yy">Full - DD, d MM, yy</option>
        <option value="'day' d 'of' MM 'in the year' yy">With text - 'day' d 'of' MM 'in the year' yy</option>
    </select>
    </p>
    
    
    </body>
    </html>
    
    0 讨论(0)
  • 2020-11-21 17:05

    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"/>
    
    0 讨论(0)
  • 2020-11-21 17:05

    Below code might help to check if form got more than 1 date field:

    <!doctype html>
    
    <html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>jQuery UI Datepicker - Display month &amp; year menus</title>
        <link rel="stylesheet" href="jquery-ui.css" />
        <script src="jquery-1.8.3.js"></script>
        <script src="jquery-ui.js"></script>
        <link rel="stylesheet" href="style.css" />
        <script>
        function pickDate(DateObject){
    //      alert(DateObject.name)
           $(function() {
                   $("#"+DateObject.name).datepicker({ dateFormat: "dd/mm/yy" }).val()
           });
        }
    /*
        $(function() {
            $( "#datepicker" ).datepicker({
                changeMonth: true,
                changeYear: true
            });
        });
    */
        </script>
    </head>
    <body>
    
    <p>From Date: <input type="text" name="FromDate" id="FromDate" size="9" onfocus="pickDate(this)"/></p>
    <p>To Date: <input type="text" name="ToDate" id="ToDate" size="9" onfocus="pickDate(this)" /></p>
    
    
    </body>
    </html>
    
    0 讨论(0)
  • 2020-11-21 17:06

    getDate function returns a JavaScript date. Use the following code to format this date:

    var dateObject = $("#datepicker").datepicker("getDate");
    var dateString = $.datepicker.formatDate("dd-mm-yy", dateObject);
    

    It uses a utility function which is built into datepicker:

    $.datepicker.formatDate( format, date, settings ) - Format a date into a string value with a specified format.

    The full list of format specifiers is available here.

    0 讨论(0)
  • 2020-11-21 17:07

    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.

    0 讨论(0)
  • 2020-11-21 17:08

    If you setup a datepicker on an input[type="text"] element you may not get a consistently formatted date, particularly when the user doesn't follow the date format for data entry.

    For example, when you set the dateFormat as dd-mm-yy and the user types 1-1-1. The datepicker will convert this to Jan 01, 2001 internally but calling val() on the datepicker object will return the string "1-1-1" -- exactly what is in the text field.

    This implies you should either be validating the user input to ensure the date entered is in the format you expect or not allowing the user to enter dates freeform (preferring instead the calendar picker). Even so it is possible to force the datepicker code to give you a string formatted like you expect:

    var picker = $('#datepicker');
    
    picker.datepicker({ dateFormat: 'dd-mm-yy' });
    
    picker.val( '1-1-1' );  // simulate user input
    
    alert( picker.val() );  // "1-1-1"
    
    var d = picker.datepicker( 'getDate' );
    var f = picker.datepicker( 'option', 'dateFormat' );
    var v = $.datepicker.formatDate( f, d );
    
    alert( v );             // "01-01-2001"
    

    Be aware however that while the datepicker's getDate() method will return a date, it can only do so much with user input that doesn't exactly match the date format. This means in the absence of validation it is possible to get a date back that is different from what the user expects. Caveat emptor.

    0 讨论(0)
提交回复
热议问题