fast-date-format

June 16, 2025 · View on GitHub

Build Status Coverage Status NPM version

fast-date-format is significantly faster than other date formatting modules.

Installation

npm install fast-date-format

Example

import { DateFormatter } from 'fast-date-format';

const dateFormat = new DateFormatter('YYYYMMDD');

dateFormat.format();
dateFormat.format(new Date());

Escaping

To escape characters in a format, surround the characters with square brackets.

const dateFormat = new DateFormatter('[Month: ]MMMM');

dateFormat.format(); // Month: December

Tokens

Supported formatting tokens:

TokenExampleDescription
YYYY2018Year
YY182 Digit Year
MMMMJanuary..DecemberMonth name
MMMJan..Dec3 letter month name
MM01..12Month
M1..12Month
HH00..23Hours (24 hour time)
H0..23Hours (24 hour time)
hh01..12Hours (12 hour time)
h1..12Hours (12 hour time)
kk01..24Hours (24 hour time)
k1..24Hours (24 hour time)
Aam, pmPost or ante meridiem
ddddMonday..SundayDay name
dddMon..Sun3 letter day name
DDDD001..365Day of year
DDD1..365Day of year
DD01..31Day of month
D1..31Day of month
E1..7Day of week
m0..59Minutes
mm00..59Minutes
s0..59Seconds
ss00..59Seconds
SSS000..999Milliseconds
ZZ-02:00Offset from UTC
Z-0200Offset from UTC
x1528643900952Unix timestamp in ms

API

DateFormatter(options)

Creates a new date formatter.

options

Either a date format as a string or an object with the following properties:

dateFormat

A date format as a string.

cache (optional)

Enables caching to increase formatting speed if set to true. Defaults to false.

DateFormatter#format([date])

Formats the date according to the dateFormat. If no date is passed the current date is used.

DateFormatter#addLocale(language, data)

Adds translations for the names of months and weekdays.

dateFormat.addLocale('en', {
  weekdays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
  months: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
})

Three letter abbreviations of the names are being generated from the months and weekdays.

DateFormatter#setLocale(language)

Changes the locale of the DateFormat instace to language. Build-in languages: en, de & it.

dateFormat.setLocale('en')

Benchmark

The benchmark has been run with Node.js v24.2.0.

$ node benchmark/benchmark.js
moment x 1,063,970 ops/sec ±0.76% (90 runs sampled)
date-format x 1,209,666 ops/sec ±0.29% (96 runs sampled)
dateformat x 557,058 ops/sec ±0.36% (95 runs sampled)
fast-date-format x 10,317,337 ops/sec ±0.41% (97 runs sampled)
fecha x 740,233 ops/sec ±0.82% (52 runs sampled)
speed-date x 9,729,244 ops/sec ±0.47% (97 runs sampled)
date-fns x 417,645 ops/sec ±0.33% (92 runs sampled)
Fastest is fast-date-format

With activated cache option:

$ node benchmark/benchmarkCache.js
moment x 1,411,803 ops/sec ±0.51% (99 runs sampled)
date-format x 1,686,897 ops/sec ±0.28% (81 runs sampled)
dateformat x 822,427 ops/sec ±0.90% (96 runs sampled)
fast-date-format x 14,652,810 ops/sec ±0.40% (95 runs sampled)
fecha x 972,796 ops/sec ±0.34% (99 runs sampled)
speed-date x 13,002,381 ops/sec ±0.43% (95 runs sampled)
date-fns x 587,636 ops/sec ±0.31% (100 runs sampled)
Fastest is fast-date-format

License

MIT