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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@fraserelliott/date-formatter

v1.0.2

Published

Configurable date formatting from a constrained format string spec

Readme

Date Format Spec Utility

A small, spec-driven date formatting utility for JavaScript.

This library formats dates using a constrained, documented format string designed for settings-driven usage (e.g. user preferences, locale presets), not free-text parsing.

The goal is predictability, clarity, and ease of extension — not maximum flexibility.


Installation

npm install @fraserelliott/date-formatter

Basic Usage

import { formatDate } from "@fraserelliott/date-formatter";

formatDate(new Date("2026-01-30"), "DD/MM/YYYY");
// → "30/01/2026"

formatDate(new Date("2026-01-30"), "WWW DD.MM.YYYY");
// → "Fri 30.01.2026"

You can also parse a format once and reuse it:

import {
  parseDateFormatString,
  formatDate,
} from "@fraserelliott/date-formatter";

const parsed = parseDateFormatString("WWWW DD MMM YYYY");
formatDate(new Date(), parsed);

Format String Spec (v1)

Format strings describe what to render, not how to guess intent. Invalid formats throw errors.

General Rules

  • Format strings are case-insensitive

  • A format consists of:

    • an optional weekday prefix
    • followed by a date format
  • All separators are one or more non-word characters (\W+)

  • Separators must be consistent within the date portion


Weekday Prefix (Optional)

If present, the weekday must appear at the start of the format string.

| Token | Meaning | | ------ | -------------------------------------- | | WWW | Short weekday name (Mon, Fri) | | WWWW | Long weekday name (Monday, Friday) |

Rules:

  • If the string starts with W, it must be a valid weekday token
  • The weekday token must be followed by a separator, this can be different to the separator used between the date tokens
  • Invalid weekday tokens throw an error

Examples:

WWW DD.MM.YYYY
WWWW - DD - MM - YYYY

Date Tokens (Mandatory)

The date portion must include exactly one day, month, and year token.

Day

| Token | Meaning | | ----- | ------------------------- | | D | Day of month (numeric) | | DD | Day of month, zero-padded |

Month

| Token | Meaning | | ------ | --------------------------- | | M | Month number | | MM | Month number, zero-padded | | MMM | Short month name (Jan) | | MMMM | Long month name (January) |

Year

| Token | Meaning | | ------ | --------------- | | YY | Two-digit year | | YYYY | Four-digit year |


Separators

  • A separator is defined as one or more non-word characters (\W+)
  • Multi-character separators are supported
  • The same separator must be used consistently between date tokens

Valid examples:

DD/MM/YYYY
DD - MM - YYYY
DD.MM.YYYY

Invalid examples:

DD-MM / YYYY
DD_MM_YYYY

Examples

formatDate(new Date("2026-01-30"), "DD/MM/YYYY");
// 30/01/2026

formatDate(new Date("2026-01-30"), "DD - MMM - YY");
// 30 - Jan - 26

formatDate(new Date("2026-01-30"), "WWW DD.MM.YYYY");
// Fri 30.01.2026

formatDate(new Date("2026-01-30"), "WWWW DD MMMM YYYY");
// Friday 30 January 2026

Errors

This library throws errors for invalid format strings, including:

  • Invalid weekday tokens (W, WW)
  • Weekday tokens not followed by a separator
  • Missing day, month, or year tokens
  • Unsupported token lengths
  • Inconsistent or missing separators

This is intentional: format strings are treated as configuration, not user input.


API

parseDateFormatString(format: string)

Parses and validates a format string.

Returns a structured format object suitable for reuse.

Throws on invalid formats.


formatDate(date, format)

Formats a date.

  • date: Date | string | number
  • format: format string or parsed format object

Returns a formatted string.


Non-Goals

This library does not aim to:

  • Parse arbitrary user-entered date formats
  • Guess intent from ambiguous strings
  • Support time or timezone tokens
  • Replicate spreadsheet or locale-magic formatting

Future Directions

Possible future extensions (not yet implemented):

  • Allow optional/missing date tokens
  • Custom padding rules
  • Locale-aware month/weekday output
  • Time formatting

The current spec is intentionally small and strict.


License

MIT