rememberDatePickerState

Functions summary

DatePickerState
@RequiresApi(value = 26)
@Composable
rememberDatePickerState(
    initialSelectedDate: LocalDate?,
    initialDisplayedMonth: YearMonth?,
    yearRange: IntRange,
    initialDisplayMode: DisplayMode,
    selectableDates: SelectableDates
)

Creates a DatePickerState for a DatePicker that is remembered across compositions.

android
DatePickerState
@Composable
rememberDatePickerState(
    initialSelectedDateMillis: Long?,
    initialDisplayedMonthMillis: Long?,
    yearRange: IntRange,
    initialDisplayMode: DisplayMode,
    selectableDates: SelectableDates
)

Creates a DatePickerState for a DatePicker that is remembered across compositions.

Cmn

Functions

rememberDatePickerState

@RequiresApi(value = 26)
@Composable
fun rememberDatePickerState(
    initialSelectedDate: LocalDate?,
    initialDisplayedMonth: YearMonth? = initialSelectedDate?.let { YearMonth.from(it) },
    yearRange: IntRange = DatePickerDefaults.YearRange,
    initialDisplayMode: DisplayMode = DisplayMode.Picker,
    selectableDates: SelectableDates = DatePickerDefaults.AllDates
): DatePickerState

Creates a DatePickerState for a DatePicker that is remembered across compositions.

Note: This composable uses LocalDate and YearMonth. For a lower-level composable that uses UTC milliseconds, see the other rememberDatePickerState overload.

The initial values are converted to UTC milliseconds at the start of the day (midnight):

To create a date picker state outside composition, see the DatePickerState function.

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.DatePicker
import androidx.compose.material3.DatePickerState
import androidx.compose.material3.Text
import androidx.compose.material3.getSelectedDate
import androidx.compose.material3.rememberDatePickerState
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

Column(
    modifier = Modifier.verticalScroll(rememberScrollState()),
    verticalArrangement = Arrangement.spacedBy(8.dp),
) {
    // Pre-select a date for April 15, 2023
    val datePickerState =
        rememberDatePickerState(initialSelectedDate = LocalDate.of(2023, 4, 15))
    DatePicker(state = datePickerState, modifier = Modifier.padding(16.dp))

    Text(
        "Selected date: ${datePickerState.getSelectedDate() ?: "no selection"}",
        modifier = Modifier.align(Alignment.CenterHorizontally),
    )
}
Parameters
initialSelectedDate: LocalDate?

a LocalDate for an initial date selection. Provide a null to indicate no selection.

initialDisplayedMonth: YearMonth? = initialSelectedDate?.let { YearMonth.from(it) }

an optional YearMonth for an initial month that will be displayed to the user. By default, in case an initialSelectedDate is provided, the initial displayed month would be the month of the selected date. You may provide a different initial month, or null to display the current one.

yearRange: IntRange = DatePickerDefaults.YearRange

an IntRange that holds the year range that the date picker will be limited to

initialDisplayMode: DisplayMode = DisplayMode.Picker

an initial DisplayMode that this state will hold

selectableDates: SelectableDates = DatePickerDefaults.AllDates

a SelectableDates that is consulted to check if a date is allowed. In case a date is not allowed to be selected, it will appear disabled in the UI.

rememberDatePickerState

@Composable
fun rememberDatePickerState(
    initialSelectedDateMillis: Long? = null,
    initialDisplayedMonthMillis: Long? = initialSelectedDateMillis,
    yearRange: IntRange = DatePickerDefaults.YearRange,
    initialDisplayMode: DisplayMode = DisplayMode.Picker,
    selectableDates: SelectableDates = DatePickerDefaults.AllDates
): DatePickerState

Creates a DatePickerState for a DatePicker that is remembered across compositions.

To create a date picker state outside composition, see the DatePickerState function.

Parameters
initialSelectedDateMillis: Long? = null

timestamp in UTC milliseconds from the epoch that represents an initial selection of a date. Provide a null to indicate no selection.

initialDisplayedMonthMillis: Long? = initialSelectedDateMillis

timestamp in UTC milliseconds from the epoch that represents an initial selection of a month to be displayed to the user. By default, in case an initialSelectedDateMillis is provided, the initial displayed month would be the month of the selected date. Otherwise, in case null is provided, the displayed month would be the current one.

yearRange: IntRange = DatePickerDefaults.YearRange

an IntRange that holds the year range that the date picker will be limited to

initialDisplayMode: DisplayMode = DisplayMode.Picker

an initial DisplayMode that this state will hold

selectableDates: SelectableDates = DatePickerDefaults.AllDates

a SelectableDates that is consulted to check if a date is allowed. In case a date is not allowed to be selected, it will appear disabled in the UI.