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

前端 未结 15 1715
清酒与你
清酒与你 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 04:44

    Google Chrome in its last beta version finally uses the input type=date, and the format is DD-MM-YYYY.

    So there must be a way to force a specific format. I'm developing a HTML5 web page and the date searches now fail with different formats.

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

    I believe the browser will use the local date format. Don't think it's possible to change. You could of course use a custom date picker.

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

    I know it's an old post but it come as first suggestion in google search, short answer no, recommended answer user a custom date piker , the correct answer that i use is using a text box to simulate the date input and do any format you want, here is the code

    <html>
    <body>
    date : 
    <span style="position: relative;display: inline-block;border: 1px solid #a9a9a9;height: 24px;width: 500px">
        <input type="date" class="xDateContainer" onchange="setCorrect(this,'xTime');" style="position: absolute; opacity: 0.0;height: 100%;width: 100%;"><input type="text" id="xTime" name="xTime" value="dd / mm / yyyy" style="border: none;height: 90%;" tabindex="-1"><span style="display: inline-block;width: 20px;z-index: 2;float: right;padding-top: 3px;" tabindex="-1">&#9660;</span>
    </span>
    <script language="javascript">
    var matchEnterdDate=0;
    //function to set back date opacity for non supported browsers
        window.onload =function(){
            var input = document.createElement('input');
            input.setAttribute('type','date');
            input.setAttribute('value', 'some text'); 
            if(input.value === "some text"){
                allDates = document.getElementsByClassName("xDateContainer");
                matchEnterdDate=1;
                for (var i = 0; i < allDates.length; i++) {
                    allDates[i].style.opacity = "1";
                } 
            }
        }
    //function to convert enterd date to any format
    function setCorrect(xObj,xTraget){
        var date = new Date(xObj.value);
        var month = date.getMonth();
        var day = date.getDate();
        var year = date.getFullYear();
        if(month!='NaN'){
            document.getElementById(xTraget).value=day+" / "+month+" / "+year;
        }else{
            if(matchEnterdDate==1){document.getElementById(xTraget).value=xObj.value;}
        }
    }
       </script>
      </body>
    </html>
    

    1- please note that this method only work for browser that support date type.

    2- the first function in JS code is for browser that don't support date type and set the look to a normal text input.

    3- if you will use this code for multiple date inputs in your page please change the ID "xTime" of the text input in both function call and the input itself to something else and of course use the name of the input you want for the form submit.

    4-on the second function you can use any format you want instead of day+" / "+month+" / "+year for example year+" / "+month+" / "+day and in the text input use a placeholder or value as yyyy / mm / dd for the user when the page load.

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

    As previously mentioned it is officially not possible to change the format. However it is possible to style the field, so (with a little JS help) it displays the date in a format we desire. Some of the possibilities to manipulate the date input is lost this way, but if the desire to force the format is greater, this solution might be a way. A date fields stays only like that:

    <input type="date" data-date="" data-date-format="DD MMMM YYYY" value="2015-08-09">
    

    The rest is a bit of CSS and JS: http://jsfiddle.net/g7mvaosL/

    $("input").on("change", function() {
        this.setAttribute(
            "data-date",
            moment(this.value, "YYYY-MM-DD")
            .format( this.getAttribute("data-date-format") )
        )
    }).trigger("change")
    input {
        position: relative;
        width: 150px; height: 20px;
        color: white;
    }
    
    input:before {
        position: absolute;
        top: 3px; left: 3px;
        content: attr(data-date);
        display: inline-block;
        color: black;
    }
    
    input::-webkit-datetime-edit, input::-webkit-inner-spin-button, input::-webkit-clear-button {
        display: none;
    }
    
    input::-webkit-calendar-picker-indicator {
        position: absolute;
        top: 3px;
        right: 0;
        color: black;
        opacity: 1;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
    <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
    <input type="date" data-date="" data-date-format="DD MMMM YYYY" value="2015-08-09">

    It works nicely on Chrome for desktop, and Safari on iOS (especially desirable, since native date manipulators on touch screens are unbeatable IMHO). Didn't check for others, but don't expect to fail on any Webkit.

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

    Since this question was asked quite a few things have happened in the web realm, and one of the most exciting is the landing of web components. Now you can solve this issue elegantly with a custom HTML5 element designed to suit your needs. If you wish to override/change the workings of any html tag just build yours playing with the shadow dom.

    The good news is that there’s already a lot of boilerplate available so most likely you won’t need to come up with a solution from scratch. Just check what people are building and get ideas from there.

    You can start with a simple (and working) solution like datetime-input for polymer that allows you to use a tag like this one:

     <date-input date="{{date}}" timezone="[[timezone]]"></date-input>
    

    or you can get creative and pop-up complete date-pickers styled as you wish, with the formatting and locales you desire, callbacks, and your long list of options (you’ve got a whole custom API at your disposal!)

    Standards-compliant, no hacks.

    Double-check the available polyfills, what browsers/versions they support, and if it covers enough % of your user base… It's 2018, so chances are it'll surely cover most of your users.

    Hope it helps!

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

    It is impossible to change the format

    We have to differentiate between the over the wire format and the browser's presentation format.

    Wire format

    The HTML5 date input specification refers to the RFC 3339 specification, which specifies a full-date format equal to: yyyy-mm-dd. See section 5.6 of the RFC 3339 specification for more details.

    This format is used by the value HTML attribute and DOM property and is the one used when doing an ordinary form submission.

    Presentation format

    Browsers are unrestricted in how they present a date input. At the time of writing Chrome, Edge, Firefox, and Opera have date support (see here). They all display a date picker and format the text in the input field.

    Desktop devices

    For Chrome, Firefox, and Opera, the formatting of the input field's text is based on the browser's language setting. For Edge, it is based on the Windows language setting. Sadly, all web browsers ignore the date formatting configured in the operating system. To me this is very strange behaviour, and something to consider when using this input type. For example, Dutch users that have their operating system or browser language set to en-us will be shown 01/30/2019 instead of the format they are accustomed to: 30-01-2019.

    Internet Explorer 9, 10, and 11 display a text input field with the wire format.

    Mobile devices

    Specifically for Chrome on Android, the formatting is based on the Android display language. I suspect that the same is true for other browsers, though I've not been able to verify this.

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