Is there any way to change input type=“date” format?

前端 未结 15 1716
清酒与你
清酒与你 2020-11-21 04:17

I\'m working with HTML5 elements on my webpage. By default input type=\"date\" shows date as YYYY-MM-DD.

The question is, is it possible t

相关标签:
15条回答
  • 2020-11-21 05:02

    It's important to distinguish two different formats:

    • The RFC 3339/ISO 8601 "wire format": YYYY-MM-DD. According to the HTML5 specification, this is the format that must be used for the input's value upon form submission or when requested via the DOM API. It is locale and region independent.
    • The format displayed by the user interface control and accepted as user input. Browser vendors are encouraged to follow the user's preferences selection. For example, on Mac OS with the region "United States" selected in the Language & Text preferences pane, Chrome 20 uses the format "m/d/yy".

    The HTML5 specification does not include any means of overriding or manually specifying either format.

    0 讨论(0)
  • 2020-11-21 05:04

    After having read lots of discussions, I have prepared a simple solution but I don't want to use lots of Jquery and CSS, just some javascript.

    HTML Code:

    <input type="date" id="dt" onchange="mydate1();" hidden/>
    <input type="text" id="ndt"  onclick="mydate();" hidden />
    <input type="button" Value="Date" onclick="mydate();" />
    

    CSS Code:

    #dt {
      text-indent: -500px;
      height: 25px;
      width: 200px;
    }
    

    Javascript Code :

    function mydate() {
      //alert("");
      document.getElementById("dt").hidden = false;
      document.getElementById("ndt").hidden = true;
    }
    
    function mydate1() {
      d = new Date(document.getElementById("dt").value);
      dt = d.getDate();
      mn = d.getMonth();
      mn++;
      yy = d.getFullYear();
      document.getElementById("ndt").value = dt + "/" + mn + "/" + yy
      document.getElementById("ndt").hidden = false;
      document.getElementById("dt").hidden = true;
    }
    

    Output:

    0 讨论(0)
  • 2020-11-21 05:04

    As said, the <input type=date ... > is not fully implemented in most browsers, so let's talk about webkit like browsers (chrome).

    Using linux, you can change it by changing the environment variable LANG, LC_TIME don't seems to work(for me at least).

    You can type locale in a terminal to see your current values. I think the same concept can be applied to IOS.

    eg: Using:

    LANG=en_US.UTF-8 /opt/google/chrome/chrome
    

    The date is showed as mm/dd/yyyy

    Using:

    LANG=pt_BR /opt/google/chrome/chrome
    

    The date is showed as dd/mm/yyyy

    You can use http://lh.2xlibre.net/locale/pt_BR/ (change pt_BR by your locale) to create you own custom locale and format your dates as you want.

    A nice more advanced reference on how change default system date is: https://ccollins.wordpress.com/2009/01/06/how-to-change-date-formats-on-ubuntu/ and https://askubuntu.com/questions/21316/how-can-i-customize-a-system-locale

    You can see you real current date format using date:

    $ date +%x
    01-06-2015
    

    But as LC_TIME and d_fmt seems to be rejected by chrome ( and I think it's a bug in webkit or chrome ), sadly it don't work. :'(

    So, unfortunately the response, is IF LANG environment variable do not solve your problem, there is no way yet.

    0 讨论(0)
  • 2020-11-21 05:05

    Try this if you need a quick solution To make yyyy-mm-dd go "dd- Sep -2016"

    1) Create near your input one span class (act as label)

    2) Update the label everytime your date is changed by user, or when need to load from data.

    Works for webkit browser mobiles and pointer-events for IE11+ requires jQuery and Jquery Date

    $("#date_input").on("change", function () {
         $(this).css("color", "rgba(0,0,0,0)").siblings(".datepicker_label").css({ "text-align":"center", position: "absolute",left: "10px", top:"14px",width:$(this).width()}).text($(this).val().length == 0 ? "" : ($.datepicker.formatDate($(this).attr("dateformat"), new Date($(this).val()))));
            });
    #date_input{text-indent: -500px;height:25px; width:200px;}
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
    <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.min.js"></script>
    <input id ="date_input" dateformat="d M y" type="date"/>
    <span class="datepicker_label" style="pointer-events: none;"></span>

    0 讨论(0)
  • 2020-11-21 05:05

    Browsers obtain the date-input format from user's system date format.

    (Tested in supported browsers, Chrome, Edge.)

    As there is no standard defined by specs as of now to change the style of date control, its not possible to implement the same in browsers.

    However, This behavior of obtaining the date format from system settings is better and I strongly suggest, we should not try to override it. The reason is, the users will see the date-input's format same as they have configured in the system/device and which they are comfortable with or matches with their locale.

    Remember, this is just the UI format on the screen which users see, in your javascript/backend you can always keep your desired format.

    Refer google developers page on same.

    0 讨论(0)
  • 2020-11-21 05:06

    i found a way to change format ,its a tricky way, i just changed the appearance of the date input fields using just a CSS code.

    input[type="date"]::-webkit-datetime-edit, input[type="date"]::-webkit-inner-spin-button, input[type="date"]::-webkit-clear-button {
      color: #fff;
      position: relative;
    }
    
    input[type="date"]::-webkit-datetime-edit-year-field{
      position: absolute !important;
      border-left:1px solid #8c8c8c;
      padding: 2px;
      color:#000;
      left: 56px;
    }
    
    input[type="date"]::-webkit-datetime-edit-month-field{
      position: absolute !important;
      border-left:1px solid #8c8c8c;
      padding: 2px;
      color:#000;
      left: 26px;
    }
    
    
    input[type="date"]::-webkit-datetime-edit-day-field{
      position: absolute !important;
      color:#000;
      padding: 2px;
      left: 4px;
      
    }
    <input type="date" value="2019-12-07">

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