useDateFormat

A dependency-free React Hook to format date using symbols, similar to the format function found in libraries such as dayjs, momentjs, or date-fns.

Tip

By default, the formatting behavior aligns with that of dayjs, momentjs, and date-fns@^1. You can set unicodeSymbols option to true to use Unicde Standard Date Symbols, which is similar to date-fns@^2 and subsequent versions.

Demo

Try to edit the format template below and see the result.

Source

Convention Date Symbols (by default)

Format:
YYYY-MM-DD ddd HH:mm:ss.SSS
Result:
2024-09-11 Wednesday 09:07:23.095
Result:
Now is 2024-09-11 Wednesday 09:07:23 A.M.
Template:

Unicode Standard Date Symbols

Format:
yyyy-MM-dd eee HH:mm:ss.SSS
Result:
2024-09-11 Wednesday 09:07:23.095
Result:
Now is 2024-09-11 Wednesday 09:07:23 am
Template:

Usage

See API for more details.

Format Symbols

ConventionUnicodeOutputDescriptionSame
Yo-2024thOrdinal formatted year-
YYyy24Two-digit year
YYYYyyyy2024Four-digit year
MM1-12The month, beginning at 1
Mo-1st, 2nd, ..., 12thThe month, ordinal formatted-
MMMM01-12The month, 2-digits
MMMMMMJan-DecThe abbreviated month name
MMMMMMMMJanuary-DecemberThe full month name
Dd1-31The day of the month
Do-1st, 2nd, ..., 31stThe day of the month, ordinal formatted-
DDdd01-31The day of the month, 2-digits
HH0-23The hour
Ho-0th, 1st, 2nd, ..., 23rdThe hour, ordinal formatted-
HHHH00-23The hour, 2-digits
hh1-12The hour, 12-hour clock
ho-1st, 2nd, ..., 12thThe hour, 12-hour clock, ordinal formatted-
hhhh01-12The hour, 12-hour clock, 2-digits
mm0-59The minute
mo-0th, 1st, ..., 59thThe minute, ordinal formatted-
mmmm00-59The minute, 2-digits
ss0-59The second
so-0th, 1st, ..., 59thThe second, ordinal formatted-
ssss00-59The second, 2-digits
SS0-9The millisecond, 1-digits
SSSS00-99The millisecond, 2-digits
SSSSSS000-999The millisecond, 3-digits
A-AM PMThe meridiem-
AA-A.M. P.M.The meridiem, periods-
aaaaaam pmThe meridiem, lowercase
aa-a.m. p.m.The meridiem, lowercase and periods-
d-0-6The day of the week, with Sunday as 0-
ddeeeeeS-SThe min name of the local day of the week
dddeeeSun-SatThe short name of the local day of the week
ddddeeeeSunday-SaturdayThe name of the local day of the week

Meridiem can be customized by customMeridiem in options.

Source

Click links below to view source on GitHub.

API

const dateStr = useDateFormat(dateLike, format, options)

DateLike

export type DateLike = Date | number | string

Format

A string that represents the format of the output date string. YYYY-MM-DD HH:mm:ss by default.

Options

export type FormatDateOptions = { /** * Whether to use Unicode date symbols. * * @see https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table * * @defaultValue false */ unicodeSymbols?: boolean /** * The locales argument indicates the locale to use. */ locales?: Intl.LocalesArgument /** * A custom function to format the meridiem. */ customMeridiem?: ( hours: number, minutes: number, isLowercase?: boolean, hasPeriod?: boolean ) => string } export type UseDateFormatOptions = FormatDateOptions & { /** * fallback string when date is invalid */ fallback?: string }

Returns

A string that represents the formatted date string.