jQuery UI DatePicker - Change Date Format

前端 未结 28 1778
后悔当初
后悔当初 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:14

    Here is an out of the box future proof date snippet. Firefox defaults to jquery ui datepicker. Otherwise HTML5 datepicker is used. If FF ever support HTML5 type="date" the script will simply be redundant. Dont forget the three dependencies are needed in the head tag.

    <script>
      src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
    </script>
    <link rel="stylesheet"href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">  
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    
    <!--Form element uses HTML 5 type="date"-->
    <div class="form-group row">
        <label for="date" class="col-sm-2 col-form-label"Date</label>
        <div class="col-sm-10">          
           <input type="date" class="form-control" name="date" id="date" placeholder="date">
        </div>
    </div>
    
    <!--if the user is using FireFox it          
    autoconverts type='date' into type='text'. If the type is text the 
    script below will assign the jquery ui datepicker with options-->
    <script>
    $(function() 
    {
      var elem = document.createElement('input');
      elem.setAttribute('type', 'date');
    
      if ( elem.type === 'text' ) 
      {
        $('#date').datepicker();
        $( "#date" ).datepicker( "option", "dateFormat", 'yy-mm-dd' );     
      }
    });
    
    0 讨论(0)
  • 2020-11-21 17:15

    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' );
    
    0 讨论(0)
  • 2020-11-21 17:15

    You can add and try this way:

    HTML file:

    <p><input type="text" id="demoDatepicker" /></p>
    

    JavaScript file:

    $(function() {  
        $("#demoDatepicker").datepicker({
            dateFormat: 'dd/mm/yy',
            changeMonth: true,
            changeYear: true,
            constrainInput: false
        });
    });
    
    0 讨论(0)
  • 2020-11-21 17:16

    My option is given below:

    $(document).ready(function () {
      var userLang = navigator.language || navigator.userLanguage;
    
      var options = $.extend({},
        $.datepicker.regional["ja"], {
          dateFormat: "yy/mm/dd",
          changeMonth: true,
          changeYear: true,
          highlightWeek: true
        }
      );
    
      $("#japaneseCalendar").datepicker(options);
    });
    #ui-datepicker-div {
      font-size: 14px;
    }
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <link rel="stylesheet" type="text/css"
              href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.min.css">
        <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
        <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.1/i18n/jquery-ui-i18n.min.js"></script>
    </head>
    <body>
    <h3>Japanese JQuery UI Datepicker</h3>
    <input type="text" id="japaneseCalendar"/>
    
    </body>
    </html>

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

    Finally got the answer for datepicker date change method:

    $('#formatdate').change(function(){
        $('#datpicker').datepicker("option","dateFormat","yy-mm-dd");
    });
    
    0 讨论(0)
提交回复
热议问题