How to Set Date Format in HTML Date Input Tag

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

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.

How to change date format to dd/mm/yyyy in html input

There is no way to change the default date type input except using CSS and js

$("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/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.min.js"></script>
<input type="date" data-date="" data-date-format="DD/MM/YYYY" value="2020-08-29">

How to set date format in input tag as ISO (2017-06-17T21:35:07.430Z)?

I assume you are using a bootstrap datepicker? The only formats supported are mentioned here. It looks like the format you want is not supported.

If you need this format elsewhere you can always just convert the Date object in your code.

HTML5 input type date formatting

The short answer is that its not currently possible to do format the date (there is no format attribute available).

However, you probably want a solution, so here are some options that you might want to try

1) Use an <input type='text'> for display, and have a hidden <input type='date'> for when the user tries to open the date-picker (it might not be easy to open the native date picker programmatically), and have JavaScript put the date from the date-picker into the input type text

2) Just use a library with a customizable date picker like the Angular Material DatePicker with the setLocale method



Related Topics



Leave a reply



Submit