Short answer: in Jetpack Compose with Material 3, create a DatePickerState with rememberDatePickerState(), show a DatePickerDialog containing a DatePicker when the user taps a read-only text field, and read selectedDateMillis when they confirm.
Complete example: text field with a date picker
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun DatePickerFieldExample() {
var showPicker by remember { mutableStateOf(false) }
var selectedDate by remember { mutableStateOf("") }
val datePickerState = rememberDatePickerState()
OutlinedTextField(
value = selectedDate,
onValueChange = { },
readOnly = true,
label = { Text("Date") },
trailingIcon = {
IconButton(onClick = { showPicker = true }) {
Icon(Icons.Default.DateRange, contentDescription = "Pick a date")
}
},
modifier = Modifier.fillMaxWidth()
)
if (showPicker) {
DatePickerDialog(
onDismissRequest = { showPicker = false },
confirmButton = {
TextButton(onClick = {
selectedDate = datePickerState.selectedDateMillis
?.let { formatDate(it) } ?: ""
showPicker = false
}) { Text("OK") }
},
dismissButton = {
TextButton(onClick = { showPicker = false }) { Text("Cancel") }
}
) {
DatePicker(state = datePickerState)
}
}
}
fun formatDate(millis: Long): String {
val formatter = SimpleDateFormat("dd/MM/yyyy", Locale.getDefault())
formatter.timeZone = TimeZone.getTimeZone("UTC")
return formatter.format(Date(millis))
}
Key points:
selectedDateMillisis the selected day at midnight in UTC. Format it with a UTC time zone, as shown, or the date can appear one day off in some time zones.- The text field is
readOnly, so the keyboard does not open. The icon button opens the picker. - Add
import androidx.compose.runtime.getValueandsetValueifby remembershows an error.
Typed date input instead of a calendar
The Material 3 date picker can start in text input mode, where users type the date. Set the initial display mode when you create the state:
val datePickerState = rememberDatePickerState(
initialDisplayMode = DisplayMode.Input
)
By default, the picker shows a toggle so users can switch between the calendar and the keyboard input.
Set an initial date and limit the range
val now = System.currentTimeMillis()
val datePickerState = rememberDatePickerState(
initialSelectedDateMillis = now,
selectableDates = object : SelectableDates {
override fun isSelectableDate(utcTimeMillis: Long): Boolean {
return utcTimeMillis <= now // no future dates
}
}
)
This is useful for birthdays or any field that must not accept dates in the future.
Date range picker
For booking and filters, use DateRangePicker with rememberDateRangePickerState(). Read selectedStartDateMillis and selectedEndDateMillis from the state.
Common mistakes
- Formatting the date in the device’s local time zone. The result can be a day early or late. Use UTC for
selectedDateMillis. - Creating the picker state inside the
ifblock. Create it outside so the selection is kept while the dialog opens and closes. - Allowing the keyboard on the field. Use
readOnly = truewhen a picker supplies the value. - Forgetting the opt-in. Date pickers may need
@OptIn(ExperimentalMaterial3Api::class)on some Material 3 versions.
FAQ
How do I add a date picker in Jetpack Compose?
Use Material 3 DatePickerDialog with DatePicker and a rememberDatePickerState().
How do I get the selected date?
Read datePickerState.selectedDateMillis and format it, using UTC.
How do I let users type a date?
Start the picker in input mode with initialDisplayMode = DisplayMode.Input.
How do I block past or future dates?
Provide selectableDates with an isSelectableDate check.
More: Jetpack Compose tutorials and Compose navigation.