useDate

A custom hook for managing date inputs.

We also have access to the ISO format: 0000-00-00

Usage

form.tsx
'use client'
import { Calendar } from '@cerberus/icons'
import {
Field,
FieldMessage,
Input,
Label,
useDate,
type InputProps,
} from '@cerberus/react'
export function DateInput(props: InputProps) {
const date = useDate()
return (
<Field>
<Label htmlFor={props.id}>Start Date</Label>
<Input
{...props}
describedBy="help:start_date"
onChange={date.onChange}
startIcon={<Calendar />}
placeholder="DD MMM YYYY"
type="text"
value={date.value}
/>
<FieldMessage id="help:start_date">
We also have access to the ISO format: {date.ISO}
</FieldMessage>
</Field>
)
}

Helpers

Cerberus exposes the following helper functions for date formatting. These are already used internally by the useDate hook.

Date Formats

General Usage
export const DateFormats = {
get ISO() {
return 'YYYY-MM-DD'
},
get USMilitary() {
return 'DD MMM YYYY'
},
get Months() {
return MONTHS
},
}

Military Formatters

// Takes an ISO date string and returns a military date string
// 'YYYY-MM-DD' -> 'DD MMM YYYY'
export function formatISOToMilitary(input: string): string
// Takes a military date string and returns an ISO date string
// 'DD MMM YYYY' -> 'YYYY-MM-DD'
export function formatMilitaryToISO(input: string): string
// Takes a military date string and returns a military date string
// 'DD MMM YYYY' -> 'DD MMM YYYY'
export function formatMilitaryDate(input: string): string

API

export interface UseDateBase {
format: string
value: string
onChange?: InputHTMLAttributes<HTMLInputElement>['onChange']
}
export interface UseDateOptions extends UseDateBase {
initialValue?: string
}
export interface UseDateReturn extends UseDateBase {
ISO: string
}
define function useDate(options?: UseDateOptions): UseDateReturn

Arguments

The useDate hook accepts the following optional arguments:

NameDefaultDescription
format'DD MMM YYYY'The format of the date string.
initialValueAn initial value of the date string.
onChangeThe custom event handler for the date input.

Return

NameDefaultDescription
format'DD MMM YYYY'The format of the date string is.
valueThe current value of the date string.
ISOThe current value of the date string in ISO format.

On this page