npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

intl-dateformatter

v1.4.7

Published

Localized formatting a js Date using Intl.DateTimeFormat (WIP)

Downloads

310

Readme

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 @

  • https://kooiinc.github.io/dateformat/index.js
  • https://dateformat.kooi.dev/index.js
  • minified by cdn.jsdelivr.net from npm https://cdn.jsdelivr.net/npm/intl-dateformatter@latest/index.min.js

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])

  • no [template] or [moreOptions]: returns date only, formatted to current locale/time zone.
  • date: a Date Object
  • template: a string containing date/time units to print/output. See 'Date/Time-units to use in the template string' below.
    • Make sure every option unit (like yyyy, WD, MM) is surrounded by a non word character (like space, /, -, so not [a-zA-Z]).
    • You can use plain text in the template by enclosing it in {} (curly brackets), e.g. '{Today is} WD'. This is especially useful if the text contains strings which may be matched as option unit (e.g. the s in it's without curly brackets will be replaced with the date seconds value).
    • If two of the (numeric) options must not be separated in the result, put ~ (tilde) between them, e.g. 'yyyy~mm~dd' => 20221102.Use {~} 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.
    • You can also use html in the template string (e.g. '{<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 list
    • ds:/ts:[full|long|medium|short] date- and/or time style (e.g. ds:medium)
      • Note: dateStyle can be used with timeStyle and vice versa, but not with other options (e.g. weekday, hour, month, etc.), so these are ignored (in other words: you can't use them in your template: only '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.
      • Note: in case of a 12 hour clock (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.

Date/Time-units to use in the template string

  • MM: Locale dependent full month name
  • M: Locale dependent abbreviated month name
  • m: Month number (1 - 12),
  • mm: Month number two digits (01 - 12)
  • yyyy: The full year
  • yy: The year (2 digits)
  • WD: Locale dependent long day of week
  • wd: Locale dependent abbreviated day of week
  • d: 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')
    • Note the way time zone is displayed depends on the time zone name given (see tzn: in syntax)

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.