Skip to main content

useDateTime

Combined date and time picker hook that composes useCalendar and useTimePicker.

import { useDateTime } from '@gentleduck/calendar'
import { useDateTime } from '@gentleduck/calendar'

Usage

import { NativeAdapter, useDateTime } from '@gentleduck/calendar'
 
const adapter = new NativeAdapter()
 
function MyDateTimePicker() {
  const { calendar, timePicker, state } = useDateTime({
    adapter,
    defaultValue: new Date(),
    hourCycle: '12',
  })
 
  // calendar - full UseCalendar.IUseCalendarReturn (state, actions, prop getters)
  // timePicker - full UseTimePicker.IUseTimePickerReturn (state, actions, getFieldProps)
  // state.value - combined Date with both date and time
}
import { NativeAdapter, useDateTime } from '@gentleduck/calendar'
 
const adapter = new NativeAdapter()
 
function MyDateTimePicker() {
  const { calendar, timePicker, state } = useDateTime({
    adapter,
    defaultValue: new Date(),
    hourCycle: '12',
  })
 
  // calendar - full UseCalendar.IUseCalendarReturn (state, actions, prop getters)
  // timePicker - full UseTimePicker.IUseTimePickerReturn (state, actions, getFieldProps)
  // state.value - combined Date with both date and time
}

Config

UseDateTime.IConfig<TDate>

PropTypeDefaultDescription
adapterAdapter.IDateAdapter<TDate>requiredDate adapter instance
localeCalendar.ICalendarLocaleConfig-Locale and direction settings
valueTDate-Controlled datetime value
defaultValueTDate-Default datetime (uncontrolled)
onChange(value: TDate) => void-Called when date or time changes
hourCycleTime.IHourCycle'24''12' or '24' hour display
showSecondsbooleanfalseShow the seconds field
monthTDate-Controlled displayed month
defaultMonthTDate-Default month (uncontrolled)
onMonthChange(month: TDate) => void-Called when month changes
disabledTDate[] | (date) => boolean-Disabled dates
fromDateTDate-Earliest selectable date
toDateTDate-Latest selectable date
onDismiss() => void-Called on Escape

Return value

UseDateTime.IReturn<TDate>

calendar

A full UseCalendar.IUseCalendarReturn<TDate, 'single'>. See useCalendar for details.

timePicker

A full UseTimePicker.IUseTimePickerReturn. See useTimePicker for details.

state

PropertyTypeDescription
valueTDate | nullCombined date + time value

actions

MethodSignatureDescription
setValue(value: TDate) => voidSet the combined datetime