
Security News
Axios Maintainer Confirms Social Engineering Attack Behind npm Compromise
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.
@chriscdn/format-date
Advanced tools
Convert a number or string representation to a formatted date string.
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.
Install via npm:
npm install @chriscdn/format-date
Or using yarn:
yarn add @chriscdn/format-date
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.
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)
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.NoneFormatDatePreset.DateTime (default)FormatDatePreset.DateTimeShortFormatDatePreset.DateFormatDatePreset.DateMediumFormatDatePreset.DateShortepochUnit: Determines how epoch values are interpreted when passing in a number:
EpochUnit.BESTGUESSEpochUnit.SECONDSEpochUnit.MILLISECONDSEpochUnit.MICROSECONDSformatOptions: Additional options for the underlying Intl.DateTimeFormat constructor, which are merged with the selected preset. See the DateTimeFormat parameters documentation for options.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"
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"
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"
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.BESTGUESSEpochUnit.SECONDSEpochUnit.MILLISECONDSEpochUnit.MICROSECONDSformatOptions: Additional options for the underlying Intl.DateTimeFormat constructor. See the DateTimeFormat parameters documentation for options.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 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: {},
relativeTo: unknown,
};
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.BESTGUESSEpochUnit.SECONDSEpochUnit.MILLISECONDSEpochUnit.MICROSECONDSformatOptions: Additional options for the underlying Intl.RelativeTimeFormat constructor. See the RelativeTimeFormat parameters documentation for options.relativeTo Format the date relative to this date, or the current date and time if not specified. Throws an error if the value cannot be converted to a Date object.FAQs
Convert a number or string representation to a formatted date string.
The npm package @chriscdn/format-date receives a total of 27 weekly downloads. As such, @chriscdn/format-date popularity was classified as not popular.
We found that @chriscdn/format-date demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.