dateformat

intl-DateFormatter

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.

Usage

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.

Install from npm

npm install intl-dateformatter

Loading

// ------------------------------
// 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>

Examples

See this small stackblitz project for examples.

Syntax:

[imported format function](date, [template], [moreOptions])

Date/Time-units to use in the template string

Note on numeric vs 2-digit

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.