Skip to content

useDateFormat

Category
Export Size
954 B
Last Changed
5 days ago

Get the formatted date according to the string of tokens passed in, inspired by dayjs

List of all available formats (HH:mm:ss by default):

FormatOutputDescription
Yo2018thOrdinal formatted year
YY18Two-digit year
YYYY2018Four-digit year
M1-12The month, beginning at 1
Mo1st, 2nd, ..., 12thThe month, ordinal formatted
MM01-12The month, 2-digits
MMMJan-DecThe abbreviated month name
MMMMJanuary-DecemberThe full month name
D1-31The day of the month
Do1st, 2nd, ..., 31stThe day of the month, ordinal formatted
DD01-31The day of the month, 2-digits
H0-23The hour
Ho0th, 1st, 2nd, ..., 23rdThe hour, ordinal formatted
HH00-23The hour, 2-digits
h1-12The hour, 12-hour clock
ho1st, 2nd, ..., 12thThe hour, 12-hour clock, sorted
hh01-12The hour, 12-hour clock, 2-digits
m0-59The minute
mo0th, 1st, ..., 59thThe minute, ordinal formatted
mm00-59The minute, 2-digits
s0-59The second
so0th, 1st, ..., 59thThe second, ordinal formatted
ss00-59The second, 2-digits
SSS000-999The millisecond, 3-digits
AAM PMThe meridiem
AAA.M. P.M.The meridiem, periods
aam pmThe meridiem, lowercase
aaa.m. p.m.The meridiem, lowercase and periods
d0-6The day of the week, with Sunday as 0
ddS-SThe min name of the day of the week
dddSun-SatThe short name of the day of the week
ddddSunday-SaturdayThe name of the day of the week
zGMT, GMT+1The timezone with offset
zzGMT, GMT+1The timezone with offset
zzzGMT, GMT+1The timezone with offset
zzzzGMT, GMT+01:00The long timezone with offset
  • Meridiem is customizable by defining customMeridiem in options.
  • Text wrapped in brackets ([...]) is output literally as an escape sequence.

Demo

Usage

Basic

tsx
import { 
useDateFormat
} from '@reause/shared'
const
formatted
=
useDateFormat
(new
Date
(), 'YYYY-MM-DD HH:mm:ss')

Use with locales

tsx
import { 
useDateFormat
} from '@reause/shared'
const
formatted
=
useDateFormat
(new
Date
(), 'YYYY-MM-DD (ddd)', {
locales
: 'en-US' })

Use with custom meridiem

ts
function 
customMeridiem
(
hours
: number,
minutes
: number,
isLowercase
?: boolean,
hasPeriod
?: boolean) {
const
m
=
hours
> 11 ? (
isLowercase
? 'μμ' : 'ΜΜ') : (
isLowercase
? 'πμ' : 'ΠΜ')
return
hasPeriod
?
m
.
split
('').
reduce
((
acc
,
current
) =>
acc
+= `${
current
}.`, '') :
m
}
useDateFormat
('2022-01-01 05:05:05', 'hh:mm:ss A', {
customMeridiem
})
// → '05:05:05 ΠΜ'
useDateFormat
('2022-01-01 17:05:05', 'hh:mm:ss AA', {
customMeridiem
})
// → '05:05:05 Μ.Μ.'

date, formatStr and options.locales are plain read-only values — pass the state value (or ref.current) directly; the string is recomputed on every call.

Type Declarations

ts
export type DateLike = Date | number | string | undefined

export interface UseDateFormatOptions {
    locales?: Intl.LocalesArgument;
    customMeridiem?: (hours: number, minutes: number, isLowercase?: boolean, hasPeriod?: boolean) => string;
}

export type UseDateFormatReturn = string

export function formatDate(date: Date, formatStr: string, options?: UseDateFormatOptions)

export function normalizeDate(date: DateLike)

export function useDateFormat(date: DateLike, formatStr?: string, options?: UseDateFormatOptions): UseDateFormatReturn

Source

Source · Demo · VueUse

Contributors

hairyf

Changelog

v0.1.0 on
e608a - feat(ci): align CI, packaging and release with VueUse
1d83a - fix(shared): resolve useDateFormat audit findings (#731)
8fddc - chore!: remove all Vue-only Maybe* types and getter unions, adopt React Ref (#462)

Released under the MIT License. v0.1.8