timepicker-ui
4.6.0GitHub
timepicker-ui
Advanced / Forms

Date Picker in a Form

Submit ISO dates from a plain HTML form while the field shows a local format. This page documents @pglejzer/datepicker-ui, the companion of timepicker-ui.

Hidden ISO value

#

When the input has a name, the picker moves the name to a hidden input placed after it. The form submits the ISO value (YYYY-MM-DD) instead of the displayed text. The hidden input is removed and the name restored on destroy(). Pick a date and submit the form to see what it sends.

Form
<form id="booking">
<label for="checkin">Check-in</label>
<input id="checkin" name="checkin" />
<button type="submit">Submit</button>
</form>
<script type="module">
import DatepickerUI from "@pglejzer/datepicker-ui";
import "@pglejzer/datepicker-ui/index.css";
new DatepickerUI("#checkin", { ui: { format: "dd.MM.yyyy" } }).create();
document.querySelector("#booking").addEventListener("submit", (event) => {
event.preventDefault();
console.log(Object.fromEntries(new FormData(event.currentTarget)));
});
</script>

Note

The visible input gets input and change events when a value is committed, and a form reset restores the initial value. In range mode an incomplete range gives an empty string.

Two fields for a range

#

With ui.endInput, each field with a name gets its own hidden ISO input.

index.html
<input id="start" name="checkin" />
<input id="end" name="checkout" />
<script type="module">
import DatepickerUI from "@pglejzer/datepicker-ui";
import "@pglejzer/datepicker-ui/index.css";
new DatepickerUI("#start", {
calendar: { mode: "range" },
ui: { endInput: "#end" },
}).create();
</script>
Esc