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

@chriscdn/format-date

v2.0.0

Published

Convert a number or string representation to a formatted date string.

Readme

@chriscdn/format-date

Overview

Convert and format dates, including date ranges and relative dates. This library helps with the common task of parsing dates and formatting them for display.

Built on Intl.DateTimeFormat and Intl.RelativeTimeFormat, it ensures accurate internationalization while using memoization for improved performance.

Installation

Install via npm:

npm install @chriscdn/format-date

Or using yarn:

yarn add @chriscdn/format-date

Usage

Importing

import {
  formatDate,
  formatDateRange,
  formatDateRelative,
  formatDateYYYYMMDD,
  formatDateYYYYMMDDTHHMMSS,
} from "@chriscdn/format-date";

Each function accepts a date as a string, number, or Date.

When a number is provided, it can represent seconds, milliseconds, or microseconds since the epoch. The library determines the unit based on the number of digits, but allows explicit control using the epochUnit parameter.

When a string or number is provided, it is first converted into a Date instance before formatting. This means JavaScript's string parsing rules and time zone caveats apply. For details, see Date time string format.

Formatting a Date

By default, it uses the local system time zone.

const formattedDate = formatDate("2025-02-12T15:00:00");
// "February 12, 2025 at 3:00 PM" (assuming an English locale)

Options

The formatDate function accepts a second parameter with options. The following shows all options and their defaults. All options are optional:

import {
  formatDate,
  FormatDatePreset,
  type FormatDateOptions,
} from "@chriscdn/format-date";

import { EpochUnit } from "@chriscdn/to-date";

const options: FormatDateOptions = {
  locale: undefined,
  preset: FormatDatePreset.DateTime,
  epochUnit: EpochUnit.BESTGUESS,
  formatOptions: {},
};

const formattedDate = formatDate(sampleDate, options);
  • locale: Specifies the desired locale (e.g., en, de, en-US, en-CA). Hyphenated locales are preferred, but underscores (e.g., en_CA) will be automatically converted to hyphens (e.g., en-CA). If undefined, the system locale is used.
  • preset: An opinionated formatting preset:
    • FormatDatePreset.None
    • FormatDatePreset.DateTime (default)
    • FormatDatePreset.DateTimeShort
    • FormatDatePreset.Date
    • FormatDatePreset.DateMedium
    • FormatDatePreset.DateShort
  • epochUnit: Determines how epoch values are interpreted when passing in a number:
    • EpochUnit.BESTGUESS
    • EpochUnit.SECONDS
    • EpochUnit.MILLISECONDS
    • EpochUnit.MICROSECONDS
  • formatOptions: Additional options for the underlying Intl.DateTimeFormat constructor, which are merged with the selected preset. See the DateTimeFormat parameters documentation for options.

Formatting as YYYY-MM-DD

Format a date as YYYY-MM-DD. Example:

const formattedDate = formatDateYYYYMMDD("2025-02-12T00:00:00");
// "2025-02-12"

The second parameter allows specifying a time zone, which defaults to the system time zone if omitted or undefined:

const formattedDate = formatDateYYYYMMDD("2025-02-12", "America/Toronto");
// "2025-02-11"

Formatting as YYYY-MM-DDTHH:MM:SS

Format a date as YYYY-MM-DDTHH:MM:SS. Example:

const formattedDate = formatDateYYYYMMDDTHHMMSS("2025-02-12T00:00:00");
// "2025-02-12T00:00:00"

The second parameter allows specifying a time zone, which defaults to the system time zone if omitted or undefined:

const formattedDate = formatDateYYYYMMDDTHHMMSS(
  "2025-02-12",
  "America/Toronto",
);
// "2025-02-11T19:00:00"

Formatting a Date Range

Format a date range as a human-readable string.

import { formatDateRange } from "@chriscdn/format-date";

const formattedDateRange = formatDateRange("2025-02-01", "2025-02-15");
// "February 1 – 15, 2025"

Options

The formatDateRange function accepts a third parameter with options. The following shows all options and their defaults. All options are optional:

import {
  formatDateRange,
  type FormatDateRangeOptions,
} from "@chriscdn/format-date";

import { EpochUnit } from "@chriscdn/to-date";

const options: FormatDateRangeOptions = {
  locale: undefined,
  epochUnit: EpochUnit.BESTGUESS,
  formatOptions: {},
};

const formatted = formatDateRange(startDate, endDate, options);
  • locale: Specifies the desired locale (e.g., en, de, en-US, en-CA). Hyphenated locales are preferred, but underscores (e.g., en_CA) will be automatically converted to hyphens (e.g., en-CA). If undefined, the system locale is used.
  • epochUnit: Determines how epoch values are interpreted when passing in a number:
    • EpochUnit.BESTGUESS
    • EpochUnit.SECONDS
    • EpochUnit.MILLISECONDS
    • EpochUnit.MICROSECONDS
  • formatOptions: Additional options for the underlying Intl.DateTimeFormat constructor. See the DateTimeFormat parameters documentation for options.

Formatting a Relative Date

Wraps Intl.RelativeTimeFormat to generate human-readable strings relative to the current date and time.

import { formatDateRelative } from "@chriscdn/format-date";

const formattedDateRelative = formatDateRelative("2024-11-25");
// "in 4 days" (assuming today is 2024-11-21)

The function calculates the duration from the current time to the target date and selects an appropriate unit, such as seconds, minutes, hours, days, weeks, months, or years, based on the duration size. A few notes:

  • The resolution is opinionated. For example, a duration of "42 days" is interpreted as "6 weeks" and not "1 month."
  • All units are rounded to zero significant digits, except for years, which are rounded to one significant digit (e.g., "in 1.5 years").
  • Durations longer than a day are resolved as calendar days. For example, 26 hours becomes "in 2 days" (spanning two calendar days) instead of being rounded down to "in 1 day".

Options

The formatDateRelative function accepts a second parameter with options. The following shows all options and their defaults. All options are optional:

import {
  formatDateRelative,
  type FormatDateRelativeOptions,
} from "@chriscdn/format-date";

import { EpochUnit } from "@chriscdn/to-date";

const options: FormatDateRelativeOptions = {
  locale: undefined,
  unit: undefined,
  epochUnit: EpochUnit.BESTGUESS,
  formatOptions: {},
};

const formattedDate = formatDateRelative(sampleDate, options);
  • locale: Specifies the desired locale (e.g., en, de, en-US, en-CA). Hyphenated locales are preferred, but underscores (e.g., en_CA) will be automatically converted to hyphens (e.g., en-CA). If undefined, the system locale is used.
  • unit: Specifies the unit for the relative date display. For valid unit values, refer to the unit documentation. If undefined, the function will automatically choose an appropriate unit based on the duration size.
  • epochUnit: Determines how epoch values are interpreted when passing in a number:
    • EpochUnit.BESTGUESS
    • EpochUnit.SECONDS
    • EpochUnit.MILLISECONDS
    • EpochUnit.MICROSECONDS
  • formatOptions: Additional options for the underlying Intl.RelativeTimeFormat constructor. See the RelativeTimeFormat parameters documentation for options.

License

MIT