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

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

    
    

    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;
    }
    
    
    

    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.

提交回复
热议问题