Dates
/ API Reference
Dates
/ API Reference

format

The Gist

import { format } from '@weser/dates'

// Wednesday 03 March 2021, 11:45 am
format(new Date(), 'DDD dd MMMM yyyy, hh:mm a')

// Mittwoch 03 März 2021, 11:45
format(new Date(), 'DDD dd MMMM yyyy, HH:mm', {
  locale: 'de-DE',
})

Parameters

ParameterTypeDescription
dateDateThe date that is formatted.
patternstringThe pattern in which the date should be formatted.
configConfig?A config option.

Config

ParameterTypeDescription
localestring?The locale to use for the date formatting.
timeZonestring?The time zone to use for the date formatting.

Example

import { format } from '@weser/dates'

// Wednesday 03 March 2021, 09:45 pm
format(new Date(), 'DDD dd MMMM yyyy, hh:mm a', {
  timeZone: 'Australia/Sydney',
  locale: 'en-AU',
})

Returns

(string) The formatted date.

Escaping Text

Sometimes we want to add arbitary text into our pattern. The problem is: Words break if they include tokens. Therefore, it is safest to escape all words with " quotes.

import { format } from 'small-date'

// Today is Wednesday the 03. of March
format(new Date(), '"Today is" DDD "the" dd. "of" MMMM')

Pattern Tokens

All examples below use the following date: new Date(2021, 2, 3, 18, 7, 8, 9) and the en-US locale.

Token DescriptionExample
DWeekday, 1 letterW
DDWeekday, 3 lettersWed
DDDWeekday, longWednesday
dDay of the month, no padding3
ddDay of the month, padded to 203
MMonth, numeric3
MMMonth, 2 digits03
MMMMonth, 3 lettersMar
MMMMMonth, longMarch
yYear, numeric2021
yyYear, 2 digits21
yyyyYear, numeric2021
hHours, no padding6
hhHours, padded to 206
HHours in 24-format, no padding18
HHHours in 24-format, padded to 218
mMinutes, no padding7
mMinutes, padded to 207
sSeconds, no padding8
ssSeconds, padded to 208
SMilliseconds, no padding9
SSMilliseconds, padded to 209
SSSMilliseconds, padded to 3009
GEra, narrowA
GGEra, shortAD
GGGEra, longAnno Domino
ZTime zone, shortGMT+1
ZZTime short, longCentral European Standard Time
PPeriod of the day, narrowin the morning
PPPeriod of the day, shortin the morning
PPPPeriod of the day, longin the morning
aMeridiempm
On this page