Skip to content

Plugins

date-and-time adopts a plugin system. Special tokens used relatively infrequently are provided as plugins outside the main library. By adding plugins as needed, you can use those tokens in Formatter and Parser. Here, Formatter refers to the output engine used by the format function, and Parser refers to the parsing engine used by the parse, preparse, and isValid functions. These engines are extended by adding plugins as arguments to these functions.

Installation

typescript
import { format } from 'date-and-time';
import { formatter as foobar } from 'date-and-time/plugins/foobar';

format(new Date(), 'ddd, MMM DD YYYY', { plugins: [foobar] });

CommonJS

typescript
const { format } = require('date-and-time');
const foobar = require('date-and-time/plugins/foobar');

format(new Date(), 'ddd, MMM DD YYYY', { plugins: [foobar.formatter] });

day-of-week

This plugin adds tokens to the Parser for reading the day of the week. Since the day of the week does not provide information that identifies a specific date, it is a meaningless token, but it can be used to skip that portion when the string you want to read contains a day of the week.

Parser

TokenMeaningInput Examples
ddddFull day nameFriday, Sunday
dddShort day nameFri, Sun
ddVery short day nameFr, Su
typescript
import { parse } from 'date-and-time';
import { parser as day_of_week } from 'date-and-time/plugins/day-of-week';

parse(
  'Thursday, March 05, 2020', 'dddd, MMMM, D YYYY',
  { plugins: [day_of_week] }
);

microsecond

This plugin adds tokens to the Parser for reading microseconds. Since the precision of JavaScript's Date type is milliseconds, these tokens are meaningless, but they can be used to skip that portion when the string you want to read contains microseconds.

Parser

TokenMeaningInput Examples
SSSSSS6-digit milliseconds123456, 000001
SSSSS5-digit milliseconds12345, 00001
SSSS4-digit milliseconds1234, 0001
fff3-digit microseconds753, 022
ff2-digit microseconds75, 02
f1-digit microseconds7, 0
typescript
import { parse } from 'date-and-time';
import { parser as microsecond } from 'date-and-time/plugins/microsecond';

parse('12:34:56.123456', 'HH:mm:ss.SSSSSS', { plugins: [microsecond] });
parse('12:34:56 123.456', 'HH:mm:ss SSS.fff', { plugins: [microsecond] });

nanosecond

This plugin adds tokens to the Parser for reading nanoseconds. Since the precision of JavaScript's Date type is milliseconds, these tokens are meaningless, but they can be used to skip that portion when the string you want to read contains nanoseconds.

Parser

TokenMeaningInput Examples
SSSSSSSSS9-digit milliseconds123456789, 000000001
SSSSSSSS8-digit milliseconds12345678, 00000001
SSSSSSS7-digit milliseconds1234567, 0000001
FFF3-digit nanoseconds753, 022
FF2-digit nanoseconds75, 02
F1-digit nanoseconds7, 0
typescript
import { parse } from 'date-and-time';
import { parser as microsecond } from 'date-and-time/plugins/microsecond';
import { parser as nanosecond } from 'date-and-time/plugins/nanosecond';

parse(
  '12:34:56.123456789',
  'HH:mm:ss.SSSSSSSSS',
  { plugins: [microsecond, nanosecond] }
);

parse(
  '12:34:56 123456.789',
  'HH:mm:ss SSSSSS.FFF',
  { plugins: [microsecond, nanosecond] }
);

ordinal

This plugin adds tokens to the Formatter and Parser for outputting or reading ordinal representations of days. This ordinal representation is limited to English and is not supported for locales other than English.

Formatter

TokenMeaningOutput Examples
DDDOrdinal representation of day1st, 2nd, 3rd
typescript
import { format } from 'date-and-time';
import { formatter as ordinal } from 'date-and-time/plugins/ordinal';

format(new Date(), 'MMM DDD YYYY', { plugins: [ordinal] });
// => Jan 1st 2019

Parser

TokenMeaningInput Examples
DDDOrdinal representation of day1st, 2nd, 3rd
typescript
import { parse } from 'date-and-time';
import { parser as ordinal } from 'date-and-time/plugins/ordinal';

parse('Jan 1st 2019', 'MMM DDD YYYY', { plugins: [ordinal] });

quarter

This plugin adds a token to the Formatter for outputting the quarter of the year.

Formatter

TokenMeaningOutput Examples
QQuarter of year1, 2, 3, 4
typescript
import { format } from 'date-and-time';
import { formatter as quarter } from 'date-and-time/plugins/quarter';

format(new Date(2025, 0, 1), 'YYYY [Q]Q', { plugins: [quarter] });
// => 2025 Q1
format(new Date(2025, 9, 1), 'YYYY [Q]Q', { plugins: [quarter] });
// => 2025 Q4

timestamp

This plugin adds tokens to the Formatter for outputting Unix timestamps.

Formatter

TokenMeaningOutput Examples
tUnix timestamp (seconds)0, 1000000000
TUnix timestamp (milliseconds)0, 1000000000000
typescript
import { format } from 'date-and-time';
import { formatter as timestamp } from 'date-and-time/plugins/timestamp';

format(new Date(1000000000000), 't', { plugins: [timestamp] });
// => 1000000000
format(new Date(1000000000000), 'T', { plugins: [timestamp] });
// => 1000000000000

two-digit-year

This plugin adds tokens to the Parser for reading 2-digit years. This token identifies years based on the following rules:

  • Values of 70 or above are interpreted as 1900s
  • Values of 69 or below are interpreted as 2000s

Parser

TokenMeaningInput Examples
YY2-digit year90, 00, 08, 19
typescript
import { parse } from 'date-and-time';
import { parser as two_digit_year } from 'date-and-time/plugins/two-digit-year';

parse('Dec 25 69', 'MMM DD YY', { plugins: [two_digit_year] });
// => Dec 25 2069
parse('Dec 25 70', 'MMM DD YY', { plugins: [two_digit_year] });
// => Dec 25 1970

week

This plugin adds tokens to the Formatter for outputting ISO week dates. These tokens follow the ISO 8601 week date system, where weeks start on Monday and the first week of the year is the one that contains the first Thursday.

Formatter

TokenMeaningOutput Examples
WISO week number1, 27, 53
WWISO week number (zero-padded)01, 27, 53
GISO week year2024, 2025
GGISO week year (2-digit zero-padded)24, 25
GGGGISO week year (4-digit zero-padded)2024, 2025
typescript
import { format } from 'date-and-time';
import { formatter as week } from 'date-and-time/plugins/week';

format(new Date(2024, 0, 1), 'GGGG-[W]WW', { plugins: [week] });
// => 2024-W01

// Note: Dec 30, 2024 belongs to ISO week year 2025
format(new Date(2024, 11, 30), 'YYYY vs GGGG [W]W', { plugins: [week] });
// => 2024 vs 2025 W1

zonename

This plugin adds tokens to the Formatter for outputting timezone names. These timezone names are limited to English and are not supported for locales other than English.

Formatter

TokenMeaningOutput Examples
zShort timezone namePST, EST
zzLong timezone namePacific Standard Time
typescript
import { format } from 'date-and-time';
import { formatter as zonename } from 'date-and-time/plugins/zonename';

format(
  new Date(),
  'MMMM DD YYYY H:mm zz',
  { plugins: [zonename] }
);
// March 14 2021 1:59 Pacific Standard Time

format(
  new Date(),
  'MMMM DD YYYY H:mm z',
  { plugins: [zonename], timeZone: 'Asia/Tokyo' }
);
// March 14 2021 18:59 JST

Released under the MIT License.