Skip to main content

React Datepicker

react-datepicker is a third-party library. Please refer to its [official documentation] for more details.

Date Picker

// React Imports
import { useState } from 'react'

// MUI Imports
import Grid from '@mui/material/Grid'

// Component Imports
import CustomInput from './PickersCustomInput'
import AppReactDatepicker from '@/libs/styles/AppReactDatepicker'

const PickersBasic = () => {
// States
const [date, setDate] = useState<Date | null | undefined>(new Date())

return (
<Grid container spacing={6}>
<Grid item xs={12} md={4}>
<AppReactDatepicker
selected={date}
id='basic-input'
onChange={(date: Date) => setDate(date)}
placeholderText='Click to select a date'
customInput={<CustomInput label='Basic' />}
/>
</Grid>
<Grid item xs={12} md={4}>
<AppReactDatepicker
disabled
selected={date}
id='disabled-input'
onChange={(date: Date) => setDate(date)}
placeholderText='Click to select a date'
customInput={<CustomInput label='Disabled' />}
/>
</Grid>
<Grid item xs={12} md={4}>
<AppReactDatepicker
readOnly
selected={date}
id='read-only-input'
onChange={(date: Date) => setDate(date)}
placeholderText='Click to select a date'
customInput={<CustomInput readOnly label='Readonly' />}
/>
</Grid>
</Grid>
)
}

export default PickersBasic
Time Pickers

// React Imports
import { useState } from 'react'

// MUI Imports
import Grid from '@mui/material/Grid'

// Component Imports
import CustomInput from './PickersCustomInput'
import AppReactDatepicker from '@/libs/styles/AppReactDatepicker'

const PickersTime = () => {
// States
const [time, setTime] = useState<Date | null | undefined>(new Date())
const [dateTime, setDateTime] = useState<Date | null | undefined>(new Date())

return (
<Grid container spacing={6}>
<Grid item xs={12} lg={6}>
<AppReactDatepicker
showTimeSelect
selected={time}
timeIntervals={15}
showTimeSelectOnly
dateFormat='h:mm aa'
id='time-only-picker'
onChange={(date: Date) => setTime(date)}
customInput={<CustomInput label='Time Only' />}
/>
</Grid>
<Grid item xs={12} lg={6}>
<AppReactDatepicker
showTimeSelect
timeFormat='HH:mm'
timeIntervals={15}
selected={dateTime}
id='date-time-picker'
dateFormat='MM/dd/yyyy h:mm aa'
onChange={(date: Date) => setDateTime(date)}
customInput={<CustomInput label='Date & Time' />}
/>
</Grid>
</Grid>
)
}

export default PickersTime
Min & Max Pickers

// React Imports
import { useState } from 'react'

// MUI Imports
import Grid from '@mui/material/Grid'

// Third-party Imports
import { subDays, addDays } from 'date-fns'

// Component Imports
import CustomInput from './PickersCustomInput'
import AppReactDatepicker from '@/libs/styles/AppReactDatepicker'

const PickersMinMax = () => {
// States
const [minDate, setMinDate] = useState<Date | null | undefined>(new Date())
const [maxDate, setMaxDate] = useState<Date | null | undefined>(new Date())

return (
<Grid container spacing={6}>
<Grid item xs={12} lg={6}>
<AppReactDatepicker
id='min-date'
selected={minDate}
minDate={subDays(new Date(), 5)}
onChange={(date: Date) => setMinDate(date)}
customInput={<CustomInput label='Min Date' />}
/>
</Grid>
<Grid item xs={12} lg={6}>
<AppReactDatepicker
id='max-date'
selected={maxDate}
maxDate={addDays(new Date(), 5)}
onChange={(date: Date) => setMaxDate(date)}
customInput={<CustomInput label='Max Date' />}
/>
</Grid>
</Grid>
)
}

export default PickersMinMax
Date Range Pickers

// React Imports
import { useState, forwardRef } from 'react'

// MUI Imports
import Grid from '@mui/material/Grid'
import TextField from '@mui/material/TextField'
import type { TextFieldProps } from '@mui/material/TextField'

// Third-party Imports
import { format, addDays } from 'date-fns'

// Component Imports
import AppReactDatepicker from '@/libs/styles/AppReactDatepicker'

type CustomInputProps = TextFieldProps & {
label: string
end: Date | number
start: Date | number
}

const PickersRange = () => {
// States
const [startDate, setStartDate] = useState<Date | null | undefined>(new Date())
const [endDate, setEndDate] = useState<Date | null | undefined>(addDays(new Date(), 15))
const [startDateRange, setStartDateRange] = useState<Date | null | undefined>(new Date())
const [endDateRange, setEndDateRange] = useState<Date | null | undefined>(addDays(new Date(), 45))

const handleOnChange = (dates: any) => {
const [start, end] = dates

setStartDate(start)
setEndDate(end)
}

const handleOnChangeRange = (dates: any) => {
const [start, end] = dates

setStartDateRange(start)
setEndDateRange(end)
}

const CustomInput = forwardRef((props: CustomInputProps, ref) => {
const { label, start, end, ...rest } = props

const startDate = format(start, 'MM/dd/yyyy')
const endDate = end !== null ? ` - ${format(end, 'MM/dd/yyyy')}` : null

const value = `${startDate}${endDate !== null ? endDate : ''}`

return <TextField fullWidth inputRef={ref} {...rest} label={label} value={value} />
})

return (
<Grid container spacing={6}>
<Grid item xs={12}>
<AppReactDatepicker
selectsRange
endDate={endDate}
selected={startDate}
startDate={startDate}
id='date-range-picker'
onChange={handleOnChange}
shouldCloseOnSelect={false}
customInput={
<CustomInput label='Date Range' start={startDate as Date | number} end={endDate as Date | number} />
}
/>
</Grid>
<Grid item xs={12}>
<AppReactDatepicker
selectsRange
monthsShown={2}
endDate={endDateRange}
selected={startDateRange}
startDate={startDateRange}
shouldCloseOnSelect={false}
id='date-range-picker-months'
onChange={handleOnChangeRange}
customInput={
<CustomInput
label='Multiple Months'
end={endDateRange as Date | number}
start={startDateRange as Date | number}
/>
}
/>
</Grid>
</Grid>
)
}

export default PickersRange
Specific Range

// React Imports
import { useState } from 'react'

// MUI Imports
import Grid from '@mui/material/Grid'

// Third-party Imports
import { addDays, setHours, setMinutes } from 'date-fns'

// Component Imports
import CustomInput from './PickersCustomInput'
import AppReactDatepicker from '@/libs/styles/AppReactDatepicker'

const PickersSpecificRange = () => {
// States
const [date, setDate] = useState<Date | null | undefined>(new Date())
const [time, setTime] = useState<Date | null | undefined>(new Date())

return (
<Grid container spacing={6}>
<Grid item xs={12}>
<AppReactDatepicker
selected={date}
id='specific-date'
minDate={new Date()}
maxDate={addDays(new Date(), 5)}
onChange={(date: Date) => setDate(date)}
customInput={<CustomInput label='Specific Date Range' />}
/>
</Grid>
<Grid item xs={12}>
<AppReactDatepicker
showTimeSelect
selected={time}
id='specific-time'
dateFormat='MM/dd/yyyy h:mm aa'
onChange={(date: Date) => setTime(date)}
minTime={setHours(setMinutes(new Date(), 0), 17)}
maxTime={setHours(setMinutes(new Date(), 30), 20)}
customInput={<CustomInput label='Specific Time' />}
/>
</Grid>
</Grid>
)
}

export default PickersSpecificRange
Include & Exclude

// React Imports
import { useState } from 'react'

// MUI Imports
import Grid from '@mui/material/Grid'

// Third-party Imports
import { addDays, subDays, setHours, setMinutes } from 'date-fns'

// Component Imports
import CustomInput from './PickersCustomInput'
import AppReactDatepicker from '@/libs/styles/AppReactDatepicker'

const PickersIncludeExclude = () => {
// States
const [date, setDate] = useState<Date | null | undefined>(new Date())
const [dateExclude, setDateExclude] = useState<Date | null | undefined>(new Date())
const [time, setTime] = useState<Date | null | undefined>(setHours(setMinutes(new Date(), 0), 18))
const [timeExclude, setTimeExclude] = useState<Date | null | undefined>(setHours(setMinutes(new Date(), 0), 18))

return (
<Grid container spacing={6}>
<Grid item xs={12} lg={6}>
<AppReactDatepicker
selected={date}
id='include-dates'
onChange={(date: Date) => setDate(date)}
customInput={<CustomInput label='Include Dates' />}
includeDates={[new Date(), addDays(new Date(), 1)]}
/>
</Grid>
<Grid item xs={12} lg={6}>
<AppReactDatepicker
id='exclude-dates'
selected={dateExclude}
onChange={(date: Date) => setDateExclude(date)}
customInput={<CustomInput label='Exclude Dates' />}
excludeDates={[subDays(new Date(), 1), subDays(new Date(), 2)]}
/>
</Grid>
<Grid item xs={12} lg={6}>
<AppReactDatepicker
showTimeSelect
selected={time}
id='include-time'
dateFormat='MM/dd/yyyy h:mm aa'
onChange={(date: Date) => setTime(date)}
customInput={<CustomInput label='Include Time' />}
includeTimes={[
setHours(setMinutes(new Date(), 0), 17),
setHours(setMinutes(new Date(), 30), 18),
setHours(setMinutes(new Date(), 30), 19),
setHours(setMinutes(new Date(), 30), 17)
]}
/>
</Grid>
<Grid item xs={12} lg={6}>
<AppReactDatepicker
showTimeSelect
id='exclude-time'
selected={timeExclude}
dateFormat='MM/dd/yyyy h:mm aa'
onChange={(date: Date) => setTimeExclude(date)}
customInput={<CustomInput label='Exclude Time' />}
excludeTimes={[
setHours(setMinutes(new Date(), 0), 17),
setHours(setMinutes(new Date(), 30), 18),
setHours(setMinutes(new Date(), 30), 19),
setHours(setMinutes(new Date(), 30), 17)
]}
/>
</Grid>
</Grid>
)
}

export default PickersIncludeExclude
Month & Year Dropdowns

// React Imports
import { useState } from 'react'

// MUI Imports
import Grid from '@mui/material/Grid'

// Component Imports
import CustomInput from './PickersCustomInput'
import AppReactDatepicker from '@/libs/styles/AppReactDatepicker'

const PickersMonthYearDropdowns = () => {
// States
const [year, setYear] = useState<Date | null | undefined>(new Date())
const [month, setMonth] = useState<Date | null | undefined>(new Date())
const [monthYear, setMonthYear] = useState<Date | null | undefined>(new Date())

return (
<Grid container spacing={6}>
<Grid item xs={12} lg={6}>
<AppReactDatepicker
selected={month}
showMonthDropdown
id='month-dropdown'
placeholderText='MM-DD-YYYY'
onChange={(date: Date) => setMonth(date)}
customInput={<CustomInput label='Month Dropdown' />}
/>
</Grid>
<Grid item xs={12} lg={6}>
<AppReactDatepicker
selected={year}
showYearDropdown
id='year-dropdown'
placeholderText='MM-DD-YYYY'
onChange={(date: Date) => setYear(date)}
customInput={<CustomInput label='Year Dropdown' />}
/>
</Grid>
<Grid item xs={12} lg={6}>
<AppReactDatepicker
showYearDropdown
showMonthDropdown
selected={monthYear}
id='month-year-dropdown'
placeholderText='MM-DD-YYYY'
onChange={(date: Date) => setMonthYear(date)}
customInput={<CustomInput label='Month & Year Dropdown' />}
/>
</Grid>
</Grid>
)
}

export default PickersMonthYearDropdowns
Month, Year & Quarter

// React Imports
import { useState } from 'react'

// MUI Imports
import Grid from '@mui/material/Grid'

// Component Imports
import CustomInput from './PickersCustomInput'
import AppReactDatepicker from '@/libs/styles/AppReactDatepicker'

const PickersMonthYear = () => {
// States
const [year, setYear] = useState<Date | null | undefined>(new Date())
const [month, setMonth] = useState<Date | null | undefined>(new Date())
const [quarter, setQuarter] = useState<Date | null | undefined>(new Date())

return (
<Grid container spacing={6}>
<Grid item xs={12} lg={6}>
<AppReactDatepicker
selected={month}
id='month-picker'
showMonthYearPicker
dateFormat='MM/yyyy'
onChange={(date: Date) => setMonth(date)}
customInput={<CustomInput label='Month Picker' />}
/>
</Grid>
<Grid item xs={12} lg={6}>
<AppReactDatepicker
showYearPicker
selected={year}
id='year-picker'
dateFormat='MM/yyyy'
onChange={(date: Date) => setYear(date)}
customInput={<CustomInput label='Year Picker' />}
/>
</Grid>
<Grid item xs={12} lg={6}>
<AppReactDatepicker
selected={quarter}
id='quarter-picker'
showQuarterYearPicker
dateFormat='yyyy, QQQ'
onChange={(date: Date) => setQuarter(date)}
customInput={<CustomInput label='Quarter Picker' />}
/>
</Grid>
</Grid>
)
}

export default PickersMonthYear
Options

// React Imports
import { useState } from 'react'

// MUI Imports
import Grid from '@mui/material/Grid'

// Component Imports
import CustomInput from './PickersCustomInput'
import AppReactDatepicker from '@/libs/styles/AppReactDatepicker'

const PickersOptions = () => {
// States
const [dateOpen, setDateOpen] = useState<Date | null | undefined>(null)
const [dateClear, setDateClear] = useState<Date | null | undefined>(new Date())
const [dateFilter, setDateFilter] = useState<Date | null | undefined>(new Date())
const [dateWeekNum, setDateWeekNum] = useState<Date | null | undefined>(new Date())
const [dateTodayBtn, setDateTodayBtn] = useState<Date | null | undefined>(new Date())

const isWeekday = (date: Date) => {
const day = new Date(date).getDay()

return day !== 0 && day !== 6
}

return (
<Grid container spacing={6}>
<Grid item xs={12} lg={4}>
<AppReactDatepicker
isClearable
id='picker-clear'
selected={dateClear}
customInput={<CustomInput label='Clear' />}
onChange={(date: Date) => setDateClear(date)}
/>
</Grid>
<Grid item xs={12} lg={4}>
<AppReactDatepicker
weekLabel='Wk'
showWeekNumbers
id='picker-week-num'
selected={dateWeekNum}
onChange={(date: Date) => setDateWeekNum(date)}
customInput={<CustomInput label='Week Numbers' />}
/>
</Grid>
<Grid item xs={12} lg={4}>
<AppReactDatepicker
id='picker-filter'
selected={dateFilter}
filterDate={isWeekday}
onChange={(date: Date) => setDateFilter(date)}
customInput={<CustomInput label='Filter Dates' />}
/>
</Grid>
<Grid item xs={12} lg={4}>
<AppReactDatepicker
showYearDropdown
showMonthDropdown
selected={dateOpen}
id='picker-open-date'
openToDate={new Date('1993/09/28')}
onChange={(date: Date) => setDateOpen(date)}
customInput={<CustomInput label='Open To Date' />}
/>
</Grid>
<Grid item xs={12} lg={4}>
<AppReactDatepicker
todayButton='Today'
selected={dateTodayBtn}
id='picker-date-today-btn'
onChange={(date: Date) => setDateTodayBtn(date)}
customInput={<CustomInput label='Date Today Button' />}
/>
</Grid>
</Grid>
)
}

export default PickersOptions
Callbacks

// React Imports
import { useState } from 'react'

// MUI Imports
import Grid from '@mui/material/Grid'

// Third-party Imports
import { toast } from 'react-toastify'

// Component Imports
import CustomInput from './PickersCustomInput'
import AppReactDatepicker from '@/libs/styles/AppReactDatepicker'

const PickersCallbacks = () => {
// States
const [date, setDate] = useState<Date | null | undefined>(new Date())

const handlePickerCallback = (msg: string) => {
toast(msg, { autoClose: 2000 })
}

return (
<Grid container spacing={6}>
<Grid item xs={12} lg={6}>
<AppReactDatepicker
selected={date}
id='callback-open'
dateFormat='MM/dd/yyyy'
onChange={(date: Date) => setDate(date)}
customInput={<CustomInput label='Open & Closed' />}
onCalendarOpen={() => handlePickerCallback(`Selected Date: ${new Date(date || '').toLocaleDateString()}`)}
onCalendarClose={() => handlePickerCallback(`Selected Date: ${new Date(date || '').toLocaleDateString()}`)}
/>
</Grid>
<Grid item xs={12} lg={6}>
<AppReactDatepicker
selected={date}
id='callback-blur'
onChange={(date: Date) => setDate(date)}
customInput={<CustomInput label='Blur' />}
onBlur={() => handlePickerCallback('Picker Closed')}
/>
</Grid>
<Grid item xs={12} lg={6}>
<AppReactDatepicker
selected={date}
id='callback-change'
customInput={<CustomInput label='onChange' />}
onChange={(date: Date) => {
setDate(date)
handlePickerCallback(`Selected Date: ${new Date(date || '').toLocaleDateString()}`)
}}
/>
</Grid>
</Grid>
)
}

export default PickersCallbacks
Customization

// React Imports
import { useState } from 'react'

// MUI Imports
import Grid from '@mui/material/Grid'

// Third-party Imports
import { subDays, addDays } from 'date-fns'

// Component Imports
import CustomInput from './PickersCustomInput'
import AppReactDatepicker from '@/libs/styles/AppReactDatepicker'

const PickersCustomization = () => {
// States
const [dateFormat, setDateFormat] = useState<Date | null | undefined>(new Date())
const [dateHighlight, setDateHighlight] = useState<Date | null | undefined>(new Date())

return (
<Grid container spacing={6}>
<Grid item xs={12} lg={6}>
<AppReactDatepicker
id='custom-format'
selected={dateFormat}
dateFormat='MMMM d, yyyy'
onChange={(date: Date) => setDateFormat(date)}
customInput={<CustomInput label='Custom Date Format' />}
/>
</Grid>
<Grid item xs={12} lg={6}>
<AppReactDatepicker
id='highlight-dates'
selected={dateHighlight}
onChange={(date: Date) => setDateHighlight(date)}
customInput={<CustomInput label='Highlight Dates' />}
highlightDates={[subDays(new Date(), 7), addDays(new Date(), 7)]}
/>
</Grid>
</Grid>
)
}

export default PickersCustomization