Date, Time & Color Inputs
By the end of this lesson you will be able to build booking forms with date, time and colour pickers that send clean, predictable values, limit the choices with min, max and step, avoid the time-zone trap that catches many Indian developers, and give a fallback where a browser has no picker.
Input Types & Attributes introduced these types in one short table. Here you will learn how each one really behaves.
One format on the wire, many on the screen
Indian Railways prints departure times as 18:30 on every ticket, whether you say "6:30 in the evening" or "saadhe chhe baje". The spoken form changes; the printed form does not.
Date and time inputs work the same way. The picker shows dates in the format of the user's browser or operating system settings: 20/11/2026 for many Indian users, 11/20/2026 for a US laptop. But the value, which your script reads and the form sends, always uses one fixed format based on ISO 8601, the international standard for dates. Your page's lang attribute does not change either of them.
| Type | Value format | Example value | Default step |
|---|---|---|---|
date | YYYY-MM-DD | 2026-11-20 | 1 day |
time | HH:MM (24-hour) | 18:30 | 60 seconds |
datetime-local | YYYY-MM-DDTHH:MM | 2026-11-20T18:30 | 60 seconds |
month | YYYY-MM | 2026-11 | 1 month |
week | YYYY-Www | 2026-W47 | 1 week |
color | #rrggbb | #e44d26 | none |
Seconds appear in time and datetime-local values only when you set a step smaller than 60, for example step="1". A week value follows ISO weeks: weeks start on Monday, and week 1 is the week that contains the year's first Thursday. So 2026-W47 is Monday 16 to Sunday 22 November 2026.
A useful bonus: because these formats put the biggest unit first, you can compare two values of the same type as plain strings. "2026-09-30" < "2026-10-01" is true, just as the dates are.
Live coding: a homestay booking in Kochi
Step 1: add the date fields. Check-in and check-out are type="date" inputs with labels.
Step 2: block past dates. Set min to today with a script. This is where many people slip. new Date().toISOString() returns the date in UTC, and India is 5 hours 30 minutes ahead of UTC. Between midnight and 5:30 am IST, UTC is still on the previous day, so your "today" would be yesterday. Build the date from local parts instead.
Step 3: link the fields. When check-in changes, check-out's min becomes the next day.
Step 4: add an arrival time from 12:00 to 21:00 in 30-minute steps (step="1800" seconds).
Step 5: show the result in the user's friendly format and in the exact values that would be sent.
<!DOCTYPE html>
<html lang="en-IN">
<head>
<meta charset="utf-8">
<title>Fort Kochi Homestay</title>
<style> label { display: block; margin-top: 10px; } pre { background: #f4f4f4; padding: 8px; } </style>
</head>
<body>
<h1>Book a room, Fort Kochi</h1>
<form id="stay">
<label for="checkin">Check-in</label>
<input id="checkin" name="checkin" type="date" required>
<label for="checkout">Check-out</label>
<input id="checkout" name="checkout" type="date" required>
<label for="arrive">Arrival time (12 pm to 9 pm, every 30 minutes)</label>
<input id="arrive" name="arrive" type="time" min="12:00" max="21:00" step="1800" value="14:00">
<p><button>Check availability</button></p>
</form>
<pre id="out"></pre>
<script>
// Format a Date as YYYY-MM-DD using local time, not UTC
function localISO(d) {
const pad = (n) => String(n).padStart(2, "0");
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`;
}
const checkin = document.querySelector("#checkin");
const checkout = document.querySelector("#checkout");
checkin.min = localISO(new Date());
checkin.addEventListener("change", () => {
const next = new Date(checkin.value + "T00:00"); // local midnight
next.setDate(next.getDate() + 1);
checkout.min = localISO(next);
if (checkout.value && checkout.value < checkout.min) checkout.value = "";
});
document.querySelector("#stay").addEventListener("submit", (e) => {
e.preventDefault(); // demo only
const nights = (Date.parse(checkout.value) - Date.parse(checkin.value)) / 86400000;
const shown = new Date(checkin.value + "T00:00").toLocaleDateString("en-IN", { dateStyle: "full" });
document.querySelector("#out").textContent =
`Friendly: ${shown}, ${nights} night(s)\n` +
`Sent: ${new URLSearchParams(new FormData(e.target))}`;
});
</script>
</body>
</html>Choose dates and submit. The friendly line reads like "Friday, 20 November 2026, 3 night(s)". The sent line shows checkin=2026-11-20&checkout=2026-11-23&arrive=14%3A00, where %3A is the encoded colon. Notice the string comparison checkout.value < checkout.min in Step 3: it works because ISO dates sort correctly as text.
datetime-local and time zones
The word local matters. A datetime-local value such as 2026-11-20T18:30 has no time zone. It means "6:30 pm wherever the user is". For a cricket academy in Pune whose visitors are all in India, that is fine. For a webinar watched from Dubai and Delhi, it is not.
In JavaScript, new Date("2026-11-20T18:30") treats the text as the browser's local time. For a user in India:
const when = new Date("2026-11-20T18:30");
console.log(when.toISOString());2026-11-20T13:00:00.000Z
The Z means UTC: 18:30 IST is 13:00 UTC. If times from many places matter, send this UTC value or the user's offset (+05:30) to the server along with the form, and store times with their zone.
month and week: support and fallbacks
month and week pickers work in Chromium browsers such as Chrome and Edge and in many mobile browsers. Firefox and Safari on desktop do not support them yet. There, the browser quietly shows a plain text box, and the user can type anything, such as "Nov 26".
You can detect this. An unsupported type falls back to text, and the type property reports it:
const test = document.createElement("input");
test.type = "month";
if (test.type === "text") {
// No month picker: show a hint and add pattern="[0-9]{4}-[0-9]{2}",
// or use two <select> elements for month and year
}For important forms, two <select> elements (month and year) work in every browser and are easy to use on phones.
The color input
type="color" opens the system colour picker. Its value is always a lowercase seven-character hex code such as #e44d26. If you give no value, or an invalid one, it is #000000 (black). Because it always has a value, required does not apply to it.
Add a list attribute pointing to a <datalist> to suggest a few colours, such as your school's house colours. Current versions of Chrome, Firefox and Safari support this and offer them as suggested colours.
The next example is a small lab. It shows every type with its live value and tells you which pickers your browser supports.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Date and colour lab</title>
<style>
label { display: block; margin-top: 8px; }
output { font-family: monospace; color: #0a5; margin-left: 8px; }
</style>
</head>
<body>
<h1>Display vs value</h1>
<label>date <input type="date" value="2026-11-20"></label>
<label>time <input type="time" value="18:30"></label>
<label>datetime-local <input type="datetime-local" value="2026-11-20T18:30"></label>
<label>month <input type="month" value="2026-11"></label>
<label>week <input type="week" value="2026-W47"></label>
<label>color (house colours) <input type="color" value="#E44D26" list="houses"></label>
<datalist id="houses">
<option value="#d32f2f"></option>
<option value="#1976d2"></option>
<option value="#388e3c"></option>
<option value="#fbc02d"></option>
</datalist>
<script>
for (const input of document.querySelectorAll("input")) {
const out = document.createElement("output");
input.after(out);
const wanted = input.getAttribute("type");
const show = () => {
const support = input.type === wanted ? "" : " (no picker: plain text box)";
out.textContent = `value = "${input.value}"${support}`;
};
input.addEventListener("input", show);
show();
}
</script>
</body>
</html>Look at the colour line: the value attribute says #E44D26, but the value reads #e44d26, because colour values are always lowercase. In Firefox or desktop Safari, the month and week lines say "no picker".
Common mistakes
- Indian date order in
value,minormax.min="20/11/2026"is ignored. Use2026-11-20. toISOString()for today's date. It gives the UTC date, which is yesterday for Indian users before 5:30 am.- Treating
datetime-localas an exact moment. It has no time zone; send the offset or a UTC value if it matters. - Assuming every browser has a month picker. Detect support and provide a fallback.
- Expecting
requiredon a colour input to force a choice. It always has a value.
Interview questions
1. A user's date picker shows 20/11/2026. What does input.value return?
"2026-11-20". The display follows the user's browser or system settings, but the value always uses the YYYY-MM-DD format, so servers and scripts get one predictable format.
2. What is the difference between datetime-local and a full date-time with a time zone?
datetime-local gives a date and time with no zone, like 2026-11-20T18:30. It cannot tell you the exact moment unless you also know the user's offset. For events across time zones, send a UTC value or the offset with it.
3. How do you detect that a browser does not support type="month"?
Create an input, set its type to "month", then read type back. If it reports "text", the browser fell back to a text box, and you should show a fallback such as two select menus.
4. Why can new Date().toISOString().slice(0, 10) give the wrong "today" in India?
toISOString() returns UTC. India is 5 hours 30 minutes ahead, so between midnight and 5:30 am IST the UTC date is still the previous day. Build the string from getFullYear(), getMonth() and getDate() instead.
5. What value does a colour input hold if you set none?
#000000. A colour input always holds a lowercase seven-character hex value, which is also why required does not apply to it.
Key points
- Pickers display dates in the user's format, but values are always ISO-style:
2026-11-20,18:30,2026-11-20T18:30,2026-11,2026-W47. stepis in days for dates, seconds for times, months formonthand weeks forweek.- Values of the same type compare correctly as strings because the biggest unit comes first.
datetime-localhas no time zone; send UTC or the offset when the exact moment matters.monthandweeklack pickers in Firefox and desktop Safari; detect support and provide a fallback.- Colour values are lowercase
#rrggbb, default#000000, andlistcan suggest swatches.
Exercise
Build a booking form for a cricket academy in Jaipur. It needs: a first practice date that cannot be in the past (set with a correct local "today"), a session start time between 6:00 and 10:00 in 30-minute steps, the month the fee is paid for (with a fallback note if the browser has no month picker), and a jersey colour with four suggested team colours. On submit, show the exact values that would be sent.
Show solution
<form id="nets">
<label for="first">First practice</label>
<input id="first" name="first" type="date" required>
<label for="start">Start time</label>
<input id="start" name="start" type="time" min="06:00" max="10:00" step="1800" required>
<label for="fee">Fee month</label>
<input id="fee" name="fee" type="month" required>
<small id="fee-hint" hidden>Type the month as YYYY-MM, for example 2026-12.</small>
<label for="jersey">Jersey colour</label>
<input id="jersey" name="jersey" type="color" value="#1976d2" list="teams">
<datalist id="teams">
<option value="#1976d2"></option><option value="#d32f2f"></option>
<option value="#fbc02d"></option><option value="#7b1fa2"></option>
</datalist>
<button>Book</button>
</form>
<p id="sent"></p>
<script>
const pad = (n) => String(n).padStart(2, "0");
const d = new Date();
document.querySelector("#first").min = `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`;
const fee = document.querySelector("#fee");
if (fee.type === "text") {
fee.pattern = "[0-9]{4}-[0-9]{2}";
document.querySelector("#fee-hint").hidden = false;
}
document.querySelector("#nets").addEventListener("submit", (e) => {
e.preventDefault();
document.querySelector("#sent").textContent = new URLSearchParams(new FormData(e.target)).toString();
});
</script>The "today" string is built from local date parts, so it is correct at any hour in India. The month input gets a pattern and a visible hint only when the browser fell back to a text box.
Quick Quiz
1. What does a datetime-local input send for 6:30 pm on 20 November 2026?
datetime-local sends a local date and time in ISO style, with a T and no time zone.
2. On a time input, what does step="1800" mean?
Time steps are in seconds; 1800 seconds is 30 minutes.
3. A page sets <input type="color" value="#FF9933">. What does input.value return?
Colour input values are normalised to lowercase hex.
4. In Firefox on a laptop, what does type="week" show?
Firefox and desktop Safari do not support week, so the input falls back to type="text".
Learn HTML with a mentor
Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.
- Project-based learning
- Mentor code reviews
- Free counselling call
