HTML: input type="time"
The time input collects a time of day, such as an appointment start time, without a date or time-zone offset.
What you will learn
- How to label a time field
- How to constrain valid times with min, max, and step
- When to use a date-time input instead
Choose between displaying a date and entering a time
Use input type="time" when someone needs to enter a time. Use the time element to mark a date or time in text.
| Setting | Rule |
|---|---|
step="60" | One-minute steps. This is the default when step is omitted. |
step="900" | 15-minute steps, such as 09:00, 09:15, and 09:30. |
step="1" | One-second steps, such as 09:30:15. |
The input’s value is the data it holds, which can differ from the screen’s format. Time values use a 24-hour format, such as 09:30 or 09:30:15. The format also supports fractional seconds. The picker’s appearance and controls depend on the browser and operating system.
<form>
<label for="alarm">Time (09:00–18:00)</label>
<input id="alarm" name="alarm" type="time"
min="09:00" max="18:00" step="900" required>
<button type="submit">Check time</button>
</form>
Compare the step and the actual value
Actual value: 09:30
09:30 matches a 15-minute step. No data is sent.
Set 09:30:15, then change the step to 1 second. A well-formed time and a time that matches the step are different conditions. This demo displays the value instead of sending it.
A time of day does not identify a worldwide moment
09:30 has no date or time zone. For appointments across regions, also specify the date and whose local time it is. datetime-local does not contain a time zone either.
Even when the browser accepts a value, check business hours and step rules again on the server. Keep the input and server in agreement about whether seconds are allowed.
Check the value on the server
These examples accept minutes or whole seconds from 09:00 through 18:00. They reject fractional seconds and extra whitespace. For a 15-minute rule, also require the number of seconds to be divisible by 900.
PHP
function secondsFromTime($value) {
if (!is_string($value) || !preg_match('/\A(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?\z/', $value)) {
return null;
}
$parts = array_map('intval', explode(':', $value));
return $parts[0] * 3600 + $parts[1] * 60 + ($parts[2] ?? 0);
}
$seconds = secondsFromTime($_POST['alarm'] ?? null);
if ($seconds === null || $seconds < 9 * 3600 || $seconds > 18 * 3600) {
http_response_code(400);
exit('Enter a time from 09:00 to 18:00.');
}
Node.js (Express)
This route assumes that an Express app exists and express.urlencoded({ extended: false }) has been configured to read the form body. Both examples read a field named alarm.
app.post('/reserve', (req, res) => {
const value = req.body?.alarm;
if (typeof value !== 'string' || value !== value.trim() ||
!/^(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?$/.test(value)) {
return res.status(400).send('Invalid time');
}
const [h, m, s = 0] = value.split(':').map(Number);
const seconds = h * 3600 + m * 60 + s;
if (seconds < 9 * 3600 || seconds > 18 * 3600) {
return res.status(400).send('Outside business hours');
}
return res.send('Time accepted');
});
A time input can also express a range across midnight, such as min="22:00" max="02:00". Check the behavior in the browsers you support.
Source: HTML time input specification.
Minimal example
<label for="opening">Opening time</label>
<input id="opening" name="opening" type="time"
min="09:00" max="18:00" step="900" required>The submitted value represents a time such as 09:00. The picker style and whether seconds are shown can vary by browser; use step when a particular precision matters.
Common mistakes
- A time alone has no date or time zone. Use a date-time design when those are important.
- Explain the relevant time zone when users may be in different regions.
- Validate business hours and other rules on the server as well as in the browser.