A utility module to format a js Date using Intl.DateTimeFormat with a formatting string
template (like ‘yyyy/mm/dd’).
Note: this module is used in the more comprehensive date module es-date-fiddler.
The module is available as ES module import @
For running in the browser one of the urls with filename DTFormat.js instead of index.js.
npm install intl-dateformatter
// ------------------------------
// Directly
// ------------------------------
// ES Module import (from jsdelivr network, index.JS, script type="module")
import dtFormat from 'https://cdn.jsdelivr.net/npm/intl-dateformatter/index.js';
// ES Module ("type": "module" in package.json)
import dtFormat from '[location of index.JS]';
// CJS require
const dtFormat = require(`[location of index.CJS]`);
// ------------------------------
// installed with npm
// ------------------------------
// CJS import
const dtFormat = require('intl-dateformatter');
// ES Import ("type": "module" in package.json)
import dtFormat from 'intl-dateformatter';
For straightforward loading this library in your browser source it to DTFormat.js.
Putting a script tag before your own script exposes the global function window.dtFormat.
<script src="//cdn.jsdelivr.net/npm/intl-dateformatter/DTFormat.js"></script>
See this small stackblitz project for examples.
[imported format function](date, [template], [moreOptions])
[template] or [moreOptions]: returns date only, formatted to
current locale/time zone.date: a Date Objecttemplate: a string containing date/time units to print/output. See ‘Date/Time-units to use in the template string’ below.
{} (curly brackets), e.g. ‘{Today is} WD’.it's without curly brackets will be replaced with the date seconds value).~ (tilde) between them, e.g. 'yyyy~mm~dd' => 20221102.{~} or ~~ to output a tilde literal in the template (e.g.: 'yyyy~~mm{~}dd' => 2022~11~02).dtf in the template prints the date formatted with the given options. If the template parameter is empty, dtf is inserted automatically.{<b>Today</b> is} <i>WD</i>’).moreOptions: a string containing one or more (comma separated) shortened option parameters, where:
l:[...]: the locale (e.g. l:en-US, l:fa-ir-u-ca-persian-nu-arabext), see this list for locales, or this page for locale extension keys, or Unicode Technical Standard #35.tzn:[short|long|shortOffset|longOffset|shortGeneric|longGeneric] the format of the timezone name (e.g.: l:en-GB,tzn:short = CET)tz:[...] the time zone (e.g. tz:Europe/Amsterdam), see column TZ database name in this listds:/ts:[full|long|medium|short] date- and/or time style (e.g. ds:medium)
dtf’ in a template will actually give you a formatted date cf the date-/timestyle you supplied).e:[long|short|narrow] the (locale specific) era representation (e.g. l:en,e:long: era in the template string is replaced with ‘Anno Domini’).h12 use 12 hour clock.hrc:[11|12|23|24] The hour cycle to use.
h12 or hrc:11/12) OR a locale using a 12 hour clock, dp in the template will be replaced by the (locale specific) day period.Spaces are allowed in the moreOptions string, e.g. l: en, tz: Asia / Shanghai, e:long.
MM: Locale dependent full month nameM: Locale dependent abbreviated month namem: Month number (1 - 12),mm: Month number two digits (01 - 12)yyyy: The full yearyy: The year (2 digits)WD: Locale dependent long day of weekwd: Locale dependent abbreviated day of weekd: Date number (1 - 31)dd: Date number 2 digits (01 - 31)h: Hour number (1 - 24)hh: Hour number 2 digits (01 - 24)mi: Minute number (0 - 59),mmi: Minute number (00 - 59),s: Second number (0 - 59)ss: Second number (00 - 59)ms: Milliseconds number (0 - 999),dp: When the locale is in a 12 hour time zone, displays the day period (AM or PM)yn: The year name (used with some calendars, like chinese or tibetan),tz: the time zone (e.g. ‘GMT+1’)
tzn: in syntax)You would expect that numeric always returns 1 digit, but this is not always the case. It depends on the locale value used.
In this library the choice is made to always return 1 digit for d, m, h, mi, and s.