DateTimePicker
Date and Time Selection
UI Components
Web Development
User Interface

DateTimePicker pick both date and time

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

A date-time picker should let the user choose a calendar date and a clock time as one logical value. The implementation depends on the platform. Some frameworks offer a combined control directly. Others require you to combine a date picker and a time picker yourself.

The Design Goal

Regardless of framework, the real requirement is usually this:

  • show or collect one date-time value
  • validate it as a single concept
  • store or transmit it consistently

That means the UI should eventually produce one object such as a timestamp, Date, or ISO string rather than two unrelated pieces that remain disconnected.

Web: Use datetime-local or a Library

For modern browsers, the simplest built-in control is:

html
<label for="appointment">Appointment time</label>
<input id="appointment" type="datetime-local" />

This gives the browser responsibility for rendering a combined date-time chooser where supported.

If you need better cross-browser consistency or more control over formatting, a library such as Flatpickr is often more predictable.

html
1<input id="appointment" type="text" />
2<script>
3flatpickr("#appointment", {
4  enableTime: true,
5  dateFormat: "Y-m-d H:i"
6});
7</script>

The library approach is usually better when localization, restrictions, or styling matter.

Android Often Uses Two Pickers

Classic Android does not really have one universal combined picker that behaves exactly like a desktop-style date-time widget. A common pattern is to ask for the date first, then the time.

kotlin
1val calendar = Calendar.getInstance()
2
3DatePickerDialog(this,
4    { _, year, month, dayOfMonth ->
5        calendar.set(year, month, dayOfMonth)
6
7        TimePickerDialog(this,
8            { _, hourOfDay, minute ->
9                calendar.set(Calendar.HOUR_OF_DAY, hourOfDay)
10                calendar.set(Calendar.MINUTE, minute)
11                println(calendar.time)
12            },
13            calendar.get(Calendar.HOUR_OF_DAY),
14            calendar.get(Calendar.MINUTE),
15            true
16        ).show()
17    },
18    calendar.get(Calendar.YEAR),
19    calendar.get(Calendar.MONTH),
20    calendar.get(Calendar.DAY_OF_MONTH)
21).show()

This is two UI steps, but one final logical result.

iOS Has a Combined Mode

On iOS, UIDatePicker can be configured for both date and time:

swift
let picker = UIDatePicker()
picker.datePickerMode = .dateAndTime
picker.preferredDatePickerStyle = .wheels

This is one of the cleaner platform examples because the framework directly models a combined picker value.

Validation Still Matters

Collecting both date and time does not automatically produce valid scheduling logic. Common rules still need to be enforced:

  • no past times
  • office hours only
  • minimum lead time
  • timezone awareness

For example, on the web you might validate before submission:

javascript
1const input = document.getElementById("appointment");
2const selected = new Date(input.value);
3
4if (selected < new Date()) {
5  alert("Please choose a future date and time.");
6}

The picker simplifies input, but it does not replace domain rules.

Time Zones Are the Subtle Part

A date-time picker usually collects local user intent. Your backend may store UTC. That conversion must be deliberate.

A recurring source of bugs is:

  • user selects local wall-clock time
  • backend interprets it as UTC directly
  • appointment shifts by several hours

So even though the control combines date and time, the full system still needs a consistent timezone policy.

Use One Value in Your Model

Even if the UI internally uses separate widgets, your view model or request model should typically use one combined field.

For example, instead of storing:

  • 'selectedDate'
  • 'selectedTime'

forever in separate places, combine them into one timestamp or DateTime object once the user finishes selection.

That makes validation, submission, and persistence much easier.

Accessibility and Input Flexibility

A good date-time picker should still allow:

  • keyboard or screen-reader use where possible
  • sensible default values
  • clear formatting
  • error feedback when input is incomplete

Fancy calendar widgets are not always automatically accessible, so test the actual component, not just its visual behavior.

Common Pitfalls

  • Treating date and time as unrelated values after the UI flow instead of combining them into one logical timestamp.
  • Assuming built-in browser or platform pickers behave identically everywhere.
  • Forgetting timezone conversion between UI input and backend storage.
  • Relying on the picker widget alone without applying business validation rules.
  • Building a custom combined picker when the platform already provides a reliable native option.

Summary

  • A date-time picker should ultimately produce one combined date-time value.
  • Some platforms offer a native combined control, while others use separate date and time pickers.
  • Built-in controls are often enough for simple cases, but libraries help when formatting and consistency matter.
  • Validation and timezone handling remain application responsibilities.
  • The best implementation is the one that keeps the user's intent clear and the stored value consistent.

Course illustration
Course illustration

All Rights Reserved.