Skip to content

format()

function format(
date: DateInput,
token?: FormatToken,
locale?: string,
opts?: FormatOptions,
): string
ParameterTypeDefaultDescription
dateDate | string | numberThe date to format
tokenstring'YYYY-MM-DDTHH:mm:ss'Format string with tokens
localestringSystem localeBCP 47 locale (e.g. 'bn-BD', 'fr')
opts.fallbackstring | null'Invalid Date'Returned when the date is invalid
import { format } from 'date-wiz';
const d = new Date('2024-05-15T14:30:07');
format(d, 'YYYY-MM-DD') // "2024-05-15"
format(d, 'DD MMMM YYYY') // "15 May 2024"
format(d, 'DD MMMM YYYY', 'en') // "15 May 2024"
format(d, 'DD MMMM', 'bn-BD') // "১৫ মে"
format(d, 'DD MMMM', 'ar') // "١٥ مايو"
format(d, 'hh:mm A') // "02:30 PM"
format(d, 'HH:mm:ss') // "14:30:07"
format(d, 'dddd, DD MMM') // "Wednesday, 15 May"
format(d, 'x') // "1715781007000" (unix ms)
format(d, 'X') // "1715781007" (unix seconds)
// Locale-aware compound tokens
format(d, 'LLL', 'en') // "May 15, 2024 at 2:30 PM"
format(d, 'LL', 'en') // "May 15, 2024"
format(d, 'L', 'en') // "5/15/2024"
format(d, 'LT', 'en') // "2:30 PM"
// Invalid date with fallback
format('bad', 'YYYY', undefined, { fallback: 'N/A' }) // "N/A"
format('bad', 'YYYY', undefined, { fallback: null }) // ""

TokenOutputExample
YYYY4-digit year2024
YY2-digit year24
MMMMFull month name (i18n)May
MMMShort month name (i18n)May
MMMonth, zero-padded05
MMonth, no padding5
DDDay of month, zero-padded05
DDay of month, no padding5
ddddFull weekday name (i18n)Wednesday
dddShort weekday name (i18n)Wed
TokenOutputExample
HH24h hours, zero-padded14
H24h hours14
hh12h hours, zero-padded02
h12h hours2
mmMinutes, zero-padded30
ssSeconds, zero-padded07
AAM/PMPM
aam/pmpm
TokenOutput
LLLFull locale date+time: May 15, 2024 at 2:30 PM
LLLocale date: May 15, 2024
LLocale short date: 5/15/2024
LTLocale time: 2:30 PM
TokenOutput
xUnix milliseconds: 1715781007000
XUnix seconds: 1715781007

  • Token matching is longest-first. MMMM is matched before MMM, MM, and M. Literal characters in the format string that don’t match any token are passed through unchanged.
  • Month and weekday names are always produced by Intl.DateTimeFormat — they automatically use the correct locale script (Arabic, Bengali, Japanese, etc.).
  • Locale compound tokens (LLL, LL, L, LT) delegate entirely to Intl.DateTimeFormat and always produce output appropriate for the given locale.