tiempo

format

Format a datetime using date-fns-like format tokens

Format a Temporal.Instant or ZonedDateTime using date-fns-like format tokens.

Signature

function format(
  input: Temporal.Instant | Temporal.ZonedDateTime,
  formatStr: string,
  options?: FormatOptions
): string

interface FormatOptions {
  locale?: string;
  timeZone?: Timezone;
}

Parameters

ParameterTypeDescription
inputTemporal.Instant | Temporal.ZonedDateTimeThe datetime to format
formatStrstringFormat string using date-fns tokens
options.localestringLocale for formatting (default: "en-US")
options.timeZoneTimezoneOverride timezone for formatting

Format Tokens

TokenExampleDescription
yyyy20254-digit year
yy252-digit year
MMMMJanuaryFull month name
MMMJanAbbreviated month name
MM012-digit month
M1Month number
Mo1stMonth with ordinal
dd202-digit day
d20Day number
do20thDay with ordinal
EEEEMondayFull weekday name
EEEMonAbbreviated weekday
HH1524-hour (2-digit)
H1524-hour
hh0312-hour (2-digit)
h312-hour
mm30Minutes (2-digit)
ss45Seconds (2-digit)
SSS123Milliseconds
aPMAM/PM (locale-aware)
zESTTimezone abbreviation
zzzzEastern Standard TimeFull timezone name
XXX-05:00Timezone offset (Z for UTC)
X+0530Timezone offset (Z for UTC); includes minutes
x+0530Timezone offset; includes minutes

The offset tokens include the minutes of the offset for zones that need them — India (+05:30) renders as +0530 and Nepal (+05:45) as +0545. The AM/PM tokens (a, aa, aaa, aaaa, aaaaa) respect the locale option, so non-English locales render their own day-period text.

Examples

Basic formatting

import { format } from '@gobrand/tiempo';

const zoned = Temporal.ZonedDateTime.from("2025-01-20T15:00:00-05:00[America/New_York]");

format(zoned, "yyyy-MM-dd");
// "2025-01-20"

format(zoned, "MMMM d, yyyy");
// "January 20, 2025"

format(zoned, "h:mm a");
// "3:00 PM"

format(zoned, "EEEE, MMMM do, yyyy 'at' h:mm a");
// "Monday, January 20th, 2025 at 3:00 PM"

With locale

format(zoned, "MMMM d, yyyy", { locale: "es-ES" });
// "enero 20, 2025"

format(zoned, "EEEE, d MMMM yyyy", { locale: "fr-FR" });
// "lundi, 20 janvier 2025"

Escaping text

Use single quotes to include literal text:

format(zoned, "'Today is' EEEE");
// "Today is Monday"

format(zoned, "MMMM do, yyyy 'at' h:mm a");
// "January 20th, 2025 at 3:00 PM"

Common Patterns

Internationalization

import { format } from '@gobrand/tiempo';

// Japanese
format(date, "yyyy年M月d日 (EEEE)", { locale: 'ja-JP' });
// "2025年1月20日 (月曜日)"

On this page