Date Picker

Let users choose a calendar date.

Preview

Your date of birth is used to calculate your age.

Usage

<FormField
control={form.control}
name="date_of_birth"
render={({ field }) => (
<FormItem className="flex flex-col">
<FormLabel>Date of birth</FormLabel>
<Popover>
<PopoverTrigger asChild>
<FormControl>
<Button
variant={"outline"}
className={cn(
"w-[240px] pl-3 text-left font-normal",
!field.value && "text-muted-foreground"
)}
>
{field.value ? (
format(field.value instanceof Date ? field.value : new Date(), "PPP")
) : (
<span>{"Pick a date"}</span>
)}
<CalendarIcon className="ml-auto h-4 w-4 opacity-50" />
</Button>
</FormControl>
</PopoverTrigger>
<PopoverContent className="w-auto p-0" align="start">
<Calendar
mode="single"
selected={field.value instanceof Date ? field.value : undefined}
onSelect={field.onChange}
initialFocus
/>
</PopoverContent>
</Popover>
<FormDescription>Your date of birth is used to calculate your age.</FormDescription>
<FormMessage />
</FormItem>
)}
/>

Details

This snippet expects the standard shadcn calendar, button, and popover primitives.

components/ui/calendar.tsx