Basic Usage
Common patterns and examples for using Chronos Picker.
Controlled Component
The most common way to use the picker is as a controlled component, managing the state in your parent component.
1import { useState } from "react";2import { DateTimePicker, DateTimeValue } from "@theengineerguy/chronos-picker";34export default function App() {5 const [value, setValue] = useState<DateTimeValue | null>(null);67 return (8 <DateTimePicker9 value={value?.dateTime}10 onChange={setValue}11 showTime={false}12 />13 );14}
Timezone Selection
You can preset a timezone or allow the user to select one. When a timezone is set, all dates displayed and selected will be in that timezone.
Zone:
1<DateTimePicker2 timezone="America/New_York"3 showTimezoneSelector4 onChange={(val) => console.log(val.iso)}5/>
Date-Only Picker
Hide the time picker and only show the calendar. Perfect for birthdays, deadlines, and date selection without time requirements.
1<DateTimePicker2 showTime={false}3 placeholder="Select date..."4 onChange={(val) => console.log(val.dateTime.toFormat('yyyy-MM-dd'))}5/>
Date Range Restrictions
Limit the selectable date range with minDate and maxDate props. Useful for booking systems, event scheduling, and date validation. Only dates within the allowed range can be selected; dates outside are disabled in the calendar.
1const [minDate, setMinDate] = useState(new Date());2const maxDate = new Date(minDate.getTime() + 30 * 24 * 60 * 60 * 1000);34// Optional: let user pick custom min date5<DateTimePicker value={minDate} onChange={(v) => setMinDate(v.dateTime.toJSDate())} showTime={false} />67{/* Show allowed range so users understand the constraint */}8<p>Selectable: {minDate.toLocaleDateString()} – {maxDate.toLocaleDateString()}</p>910<DateTimePicker11 minDate={minDate}12 maxDate={maxDate}13 placeholder="Pick a date in range"14 onChange={setValue}15 showTime={false}16/>
Date Range Picker (Start & End)
Use selectionMode="range" to select a start and end date. The calendar shows the selected range with a bar between dates and circles on start/end. A summary header displays the number of nights and the date range (e.g. "3 nights", "11 Feb 2026 – 14 Feb 2026").
1import { DateTimePicker, DateTimeRangeValue } from "@theengineerguy/chronos-picker";23const [range, setRange] = useState<DateTimeRangeValue | null>(null);45<DateTimePicker6 selectionMode="range"7 rangeValue={range ? { start: range.start.dateTime, end: range.end.dateTime } : null}8 onRangeChange={setRange}9 placeholder="Select check-in and check-out"10 showTime={false}11/>1213{/* range.start, range.end (DateTimeValue), range.nights */}
12-Hour Format
Display time in 12-hour format with AM/PM. Great for user-facing applications where 12-hour format is preferred.
1<DateTimePicker2 showTime={true}3 use24Hour={false}4 onChange={(val) => {5 // Format with AM/PM6 console.log(val.dateTime.toFormat('hh:mm a'));7 }}8/>
Orientation
Control the layout of the calendar and time panel with the orientation prop. Use portrait (default) for a stacked layout, or landscape for calendar and time side-by-side—handy on wider screens.
1{/* Stacked: calendar above, time below */}2<DateTimePicker orientation="portrait" showTime={true} onChange={setValue} />34{/* Side-by-side: calendar left, time right */}5<DateTimePicker orientation="landscape" showTime={true} onChange={setValue} />
Disabled State
Disable the picker when interaction should be prevented, such as during form submission or when conditions aren't met.
1const [isSubmitting, setIsSubmitting] = useState(false);23<DateTimePicker4 disabled={isSubmitting}5 onChange={setValue}6/>
With Default Value
Set an initial value when the component mounts. Useful for edit forms or when you want to show a pre-selected date.
1import { DateTime } from "luxon";2import { useState } from "react";34export default function App() {5 // Initialize with current date/time6 const [value, setValue] = useState<DateTimeValue>({7 dateTime: DateTime.now(),8 formatted: DateTime.now().toFormat('MMM dd, yyyy HH:mm'),9 iso: DateTime.now().toISO(),10 });1112 return <DateTimePicker value={value.dateTime} onChange={setValue} />;13}
Form Integration
Integrate with popular form libraries like React Hook Form or Formik. The picker works seamlessly with controlled form state.
1import { useForm, Controller } from "react-hook-form";2import { DateTimePicker } from "@theengineerguy/chronos-picker";34export default function MyForm() {5 const { control, handleSubmit } = useForm();67 const onSubmit = (data) => {8 console.log("Selected date:", data.appointmentDate?.iso);9 };1011 return (12 <form onSubmit={handleSubmit(onSubmit)}>13 <Controller14 name="appointmentDate"15 control={control}16 rules={{ required: "Date is required" }}17 render={({ field }) => (18 <DateTimePicker19 value={field.value?.dateTime}20 onChange={field.onChange}21 placeholder="Select appointment date"22 />23 )}24 />25 <button type="submit">Submit</button>26 </form>27 );28}
Custom Date Format
Format the output using Luxon's powerful formatting options. Access the DateTime object directly for complete control.
1<DateTimePicker2 onChange={(val) => {3 // Different format options4 console.log(val.iso); // ISO 8601: "2024-03-15T14:30:00.000-04:00"5 console.log(val.formatted); // Default format67 // Custom formats using Luxon8 const dt = val.dateTime;9 console.log(dt.toFormat('yyyy-MM-dd')); // "2024-03-15"10 console.log(dt.toFormat('MMMM dd, yyyy')); // "March 15, 2024"11 console.log(dt.toFormat('EEE, MMM d')); // "Fri, Mar 15"12 console.log(dt.toLocaleString()); // Locale-aware format1314 // For API calls15 console.log(dt.toISO()); // Full ISO string16 console.log(dt.toMillis()); // Unix timestamp (ms)17 console.log(dt.toSeconds()); // Unix timestamp (seconds)18 }}19/>
Working with Timezones
Handle timezone conversions and work with dates across different timezones. Perfect for global applications and scheduling systems.
1import { DateTime } from "luxon";23<DateTimePicker4 timezone="America/Los_Angeles"5 showTimezoneSelector6 onChange={(val) => {7 const pst = val.dateTime; // In Pacific time89 // Convert to other timezones10 const est = pst.setZone('America/New_York');11 const utc = pst.setZone('UTC');12 const tokyo = pst.setZone('Asia/Tokyo');1314 console.log('PST:', pst.toFormat('HH:mm'));15 console.log('EST:', est.toFormat('HH:mm'));16 console.log('UTC:', utc.toFormat('HH:mm'));17 console.log('Tokyo:', tokyo.toFormat('HH:mm'));1819 // Store in database (always use ISO or UTC)20 saveToDatabase(val.iso);21 }}22/>