【问题标题】:Date format for Input Field输入字段的日期格式
【发布时间】:2016-05-14 05:16:37
【问题描述】:

我刚刚创建了输入日期字段并尝试更改格式。但无法实现。这个可以改格式吗?

代码片段:

<form>
<input value="i want to show 09/09/2015" /><br><br/><br/>
<input type="date" id="dt" value="2014-09-09"/>

</form>

http://jsfiddle.net/HudMe/626/

我想显示 dd/mm/yyyy 格式。

【问题讨论】:

    标签: javascript html date input


    【解决方案1】:

    使用下面的日期选择器。 https://jqueryui.com/datepicker/

    在下面添加你的脚本。

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

    【讨论】:

      【解决方案2】:
      <html lang="en">
      <head>
        <meta charset="utf-8">
        <title>jQuery UI Datepicker - Default functionality</title>
        <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
        <script src="//code.jquery.com/jquery-1.10.2.js"></script>
        <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
        <link rel="stylesheet" href="/resources/demos/style.css">
        <script>
        $(function() {
          $( "#datepicker" ).datepicker();
        });
        </script>
      </head>
        <body>
          <p>Date: <input type="text" id="datepicker"></p>
        </body>
      </html>
      

      您可以通过以下方式更改 dat 的格式:

      $(function() {
          $( "#datepicker" ).datepicker();
          $( "#format" ).change(function() {
            $( "#datepicker" ).datepicker( "option", "dateFormat", <SPECIFY Date Formate here>);
          });
       });
      

      【讨论】:

      【解决方案3】:

      假设您要更改&lt;input type='date'&gt; 格式,因此无法更改&lt;input type="date"&gt; 中的格式。 但是您可以使用https://jqueryui.com/datepicker/ 来代替它。另见http://api.jqueryui.com/datepicker/

      在您可以更改日期格式后,如下所示:

      $.datepicker.formatDate( "yy-mm-dd", new Date( 2007, 1 - 1, 26 ) );

      【讨论】:

        【解决方案4】:

        在谷歌浏览器中它工作正常!

        火狐

        添加小js

         (function(){
            var target = document.getElementById("dt");
            var val = target.getAttribute ('value');
        
            val = val.split("-").reverse().join('-');  
            target.setAttribute('value',val);
          })();
        

        你的小提琴

        【讨论】:

          猜你喜欢
          • 2015-09-13
          • 1970-01-01
          • 2015-03-01
          • 2016-04-02
          • 2023-03-27
          • 1970-01-01
          • 2018-09-29
          • 2020-09-07
          • 2023-04-03
          相关资源
          最近更新 更多