Skip to content

format

Formats a date as a short label that does not depend on the machine: UTC, English, and Jul 27, 2026 by default. A shorthand for formatInTimezone(date, 'UTC', { locale, formatOptions }).

formatOptions replaces the default format rather than extending it, so { month: 'short', day: 'numeric' } gives Jul 27 with no year.

Returns null if the date or the locale is invalid.

Available since v3.1.5

import { format } from '@helpers4/date';
// or, from the all-in-one package (same code, one install):
import { format } from 'helpers4/date';
format(date: DateLike, formatOptions: DateTimeFormatOptions, locale: string): string | null
ParameterTypeDescription
dateDateLikeThe date to format
formatOptionsDateTimeFormatOptions`Intl.DateTimeFormat` options (default: day, short month, year)
localestringBCP 47 locale tag (default: `‘en’`)

string | null — The formatted date, or null

format('2026-07-27')                                   // => 'Jul 27, 2026'
format('2026-07-27', { month: 'short', day: 'numeric' }) // => 'Jul 27'
format('2026-07-27', undefined, 'fr')                  // => '27 juil. 2026'
format('nope')                                         // => null

A value that can be converted to a Date.

  • Date — used as-is (validated for Invalid Date)
  • number — treated as a timestamp (seconds or milliseconds, auto-detected); 0 is treated as invalid and produces null in ensureDate
  • string — parsed via new Date(string)
  • Temporal.Instant | Temporal.ZonedDateTime — read via their epochMilliseconds property
type DateLike = Date | number | string | Temporal.Instant | Temporal.ZonedDateTime

View source on GitHub