When working with web forms, you might encounter situations where the default date input behavior of HTML doesn’t align with your requirements. By default, an <input type="date">
field uses the YYYY-MM-DD
format, which is standard according to the HTML5 specification and RFC 3339 for form submissions. However, users may prefer different formats like DD-MM-YYYY
. Although altering this "wire format" isn’t possible due to specifications, there are creative ways to achieve a more user-friendly display of date inputs.
Understanding Date Input Formats
Wire Format vs Presentation Format
-
Wire Format: This is the standardized
YYYY-MM-DD
format used for form submissions and when accessing input values via JavaScript. It’s designed to be locale-independent and consistent across all browsers. -
Presentation Format: Browsers have flexibility in displaying date inputs, allowing them to adapt to different locales and user settings. However, this means users might see different formats depending on their browser or operating system language settings.
Techniques for Custom Date Presentation
While changing the wire format is off-limits, you can manipulate how dates are presented using a combination of CSS and JavaScript:
Method 1: Using CSS Pseudo-elements
You can visually alter how date fields look with CSS. For example, by applying styles to WebKit-based browsers like Chrome or Safari, you might change field appearances, though not the input itself.
input[type="date"]::-webkit-datetime-edit,
input[type="date"]::-webkit-inner-spin-button,
input[type="date"]::-webkit-clear-button {
color: #fff;
}
input[type="date"]::-webkit-datetime-edit-year-field {
position: absolute !important;
border-left: 1px solid #8c8c8c;
padding: 2px;
left: 56px;
}
input[type="date"]::-webkit-datetime-edit-month-field {
position: absolute !important;
border-left: 1px solid #8c8c8c;
padding: 2px;
left: 26px;
}
input[type="date"]::-webkit-datetime-edit-day-field {
position: absolute !important;
color: #000;
padding: 2px;
left: 4px;
}
This CSS snippet customizes the appearance of the date picker but doesn’t change how the date is formatted or interpreted by the browser.
Method 2: JavaScript for Display Manipulation
For a more interactive solution, you can use JavaScript along with libraries like Moment.js to display dates in your preferred format. This method involves capturing user input and dynamically updating another element’s text to show the desired format:
<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">
<script>
$("input").on("change", function() {
this.setAttribute(
"data-date",
moment(this.value, "YYYY-MM-DD")
.format(this.getAttribute("data-date-format"))
);
}).trigger("change");
</script>
<style>
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-calendar-picker-indicator {
position: absolute;
top: 3px;
right: 0;
color: black;
opacity: 1;
}
</style>
This approach uses jQuery to listen for changes on the date input and updates a pseudo-element with the preferred format using Moment.js.
Advanced Techniques: Web Components
For a more robust solution, consider leveraging web components. Libraries like Polymer enable developers to create custom HTML elements that encapsulate functionality, such as custom-styled date pickers:
<date-input date="{{date}}" timezone="[[timezone]]"></date-input>
By creating or using existing web components, you can provide a consistent user experience across different browsers and devices while adhering to standard practices.
Conclusion
While the HTML5 specification restricts altering the wire format of date inputs, creative use of CSS and JavaScript offers ways to improve presentation for users. Whether through simple styling tricks or more advanced solutions like custom web components, these techniques can help you build intuitive and user-friendly interfaces that respect local formatting preferences.