Formatting UTC and zoned dates with date-fns

How date-fns formats dates in UTC or a named time zone, when to use UTCDate, TZDate or formatInTimeZone, and whether the output stays meaningful once copied.

A JavaScript Date holds one instant in time. The date-fns format function turns that instant into text, and by default it uses the time zone of the machine running the code. Formatting "in UTC" or "in a time zone" therefore means choosing which wall clock the resulting string describes. This review covers core format, the UTC and zone helpers added around date-fns v4, the separate date-fns-tz package and its formatInTimeZone function, and whether the formatted text keeps its meaning after it is copied or shared. No version-specific test results or measurements were supplied for this article. The examples follow documented behavior and ordinary offset arithmetic, so check the outputs against the package versions you actually install.

Short verdict

  • UTC only: wrap the instant in UTCDate from @date-fns/utc before calling format, or use the built-in toISOString when a fixed ISO 8601 string is enough.
  • A named zone such as America/New_York: use TZDate from @date-fns/tz with date-fns v4, or formatInTimeZone from date-fns-tz. Choose one approach per codebase.
  • Main benefit: you get full control over the pattern, including tokens and locale, while the output stays consistent no matter which zone the server or browser uses.
  • Decisive tradeoff: a formatted string is only text. If the pattern leaves out an offset or a zone, the reader cannot recover the original instant.
  • When a library-free option is better: toISOString is the safer choice for machine exchange. Intl.DateTimeFormat with its timeZone option often covers simple localized display without any extra dependency.

What format actually changes

format never modifies the Date you pass in. It reads the date's local calendar fields and produces a new string. The same instant can therefore yield different text on different machines.

import { format } from "date-fns";

const instant = new Date("2024-03-10T06:30:00Z");

format(instant, "yyyy-MM-dd HH:mm");
// Machine set to UTC:                 "2024-03-10 06:30"
// Machine set to America/Los_Angeles: "2024-03-09 22:30"

Offset tokens follow the same rule. With a plain Date, X and x report the machine's own offset, not the offset of any zone you had in mind. The stored timestamp is never the problem. The question is always which zone's fields the formatter reads.

Formatting in UTC

UTCDate keeps the same timestamp as the original Date, but its getters return UTC values. Because format reads those getters, the output is UTC on every machine.

import { format } from "date-fns";
import { UTCDate } from "@date-fns/utc";

const instant = new Date("2024-03-10T06:30:00Z");
const utc = new UTCDate(instant.getTime());

format(utc, "yyyy-MM-dd HH:mm 'UTC'");
// "2024-03-10 06:30 UTC"

instant.toISOString();
// "2024-03-10T06:30:00.000Z" (built in, fixed pattern)

Text wrapped in single quotes inside the pattern is printed literally. Here it labels the zone so the reader does not have to guess.

Formatting in a named zone with date-fns v4

date-fns v4 introduced first-class time zone support through the @date-fns/tz package. A TZDate carries an IANA zone name, and format reads its fields in that zone, offset tokens included. The example instant falls half an hour before New York's 2024 daylight saving change, so the standard-time offset applies. v4 also added a context option, called in, which many functions accept together with a tz() helper. Confirm that format supports it in the version you install before relying on it.

import { format } from "date-fns";
import { TZDate } from "@date-fns/tz";

const instant = new Date("2024-03-10T06:30:00Z");
const ny = new TZDate(instant.getTime(), "America/New_York");

format(ny, "yyyy-MM-dd HH:mm xxx");
// "2024-03-10 01:30 -05:00"

date-fns-tz and formatInTimeZone

date-fns-tz is a separately maintained companion package. Its formatInTimeZone function takes a Date, a zone and a pattern, and leaves the input untouched. The package's own formatting also supports the z, zz, zzz and zzzz tokens for zone names such as abbreviations. Those names come from the runtime's Intl data, so they can differ between environments and locales. In core date-fns, the z tokens print GMT-style offsets rather than names.

import { formatInTimeZone } from "date-fns-tz";

const instant = new Date("2024-03-10T06:30:00Z");

formatInTimeZone(instant, "America/New_York", "yyyy-MM-dd HH:mm xxx");
// "2024-03-10 01:30 -05:00"

formatInTimeZone(instant, "UTC", "yyyy-MM-dd HH:mm XXX");
// "2024-03-10 06:30 Z"  (XXX prints Z for a zero offset; xxx prints +00:00)

The shifted-date trap

date-fns-tz also provides toZonedTime, named utcToZonedTime before date-fns-tz v3. Unlike the approaches above, it does change the stored value. It returns a new Date whose local fields match the target zone's wall clock, which requires shifting the underlying timestamp. Patterns without an offset look correct afterward. Offset tokens and toISOString, however, now describe a different instant. The exception is a machine that is already in the target zone.

import { toZonedTime } from "date-fns-tz";
import { format } from "date-fns";

const instant = new Date("2024-03-10T06:30:00Z");
const shifted = toZonedTime(instant, "America/New_York");

format(shifted, "yyyy-MM-dd HH:mm"); // "2024-03-10 01:30" (wall clock looks right)
format(shifted, "xxx");              // the machine's offset, not -05:00
shifted.toISOString();               // no longer the original instant

When the output needs an offset, use TZDate or formatInTimeZone instead. Reserve shifted dates for code that works only with calendar fields.

Parsing decides what gets formatted

Many formatting surprises actually come from how the date was parsed. Under the JavaScript specification, a date-only ISO string passed to the Date constructor means midnight UTC. date-fns parseISO treats the same string as local midnight.

import { format, parseISO } from "date-fns";

format(new Date("2024-03-10"), "MMM d, yyyy");
// "Mar 9, 2024" on a machine set to a US time zone

format(parseISO("2024-03-10"), "MMM d, yyyy");
// "Mar 10, 2024"

Token pitfalls

  • yyyy is the calendar year. YYYY is the week-numbering year and differs near the new year. date-fns throws on YYYY unless the useAdditionalWeekYearTokens option is set.
  • dd is the day of the month. DD is the day of the year and is guarded the same way through useAdditionalDayOfYearTokens.
  • MM is the month and mm is minutes.
  • HH is the 24-hour clock. hh is the 12-hour clock and needs an a token to show AM or PM.
  • X and XXX print Z for UTC. x and xxx always print a numeric offset.

How the approaches compare

Approach | Zone used for output | Stored timestamp changed? | Best fit
format on a plain Date | Runtime's local zone | No | Display in the viewer's own zone
format on a UTCDate | UTC | No | Logs, reports and UTC-only display
format on a TZDate | Zone attached to the TZDate | No | Named-zone display with date-fns v4
formatInTimeZone | Zone passed as an argument | No | Named-zone display with date-fns-tz
toZonedTime, then format | Target wall clock, but the offset tokens show the local offset | Yes | Field-only calculations; avoid for output that includes an offset
toISOString | UTC, fixed pattern | No | Machine exchange and storage

Will the result stay readable after copying or sharing?

Patterns built from numeric tokens and ASCII separators produce plain characters that paste cleanly. date-fns inserts only the characters in your pattern plus locale-supplied names such as month names. Intl-based output is less predictable. Recent CLDR data places a narrow no-break space before AM and PM in some locales. That output looks identical on screen but can break string comparisons, parsers and tests that expect an ordinary space. This applies to Intl.DateTimeFormat and to any zone names that date-fns-tz pulls from Intl.

Meaning is the bigger risk. A string like "Mar 10, 2024 1:30 AM" names no zone, so anyone reading it elsewhere may assume their own. Abbreviations do not fully solve this, because some are shared. CST, for example, can mean US Central Standard Time or China Standard Time. Numeric dates such as 03/10/2024 read as March 10 in the US and as 3 October in many other regions. A numeric offset or an IANA zone name removes the zone ambiguity, and year-month-day order removes the ordering ambiguity.

Screen readers and other assistive technology read the text that is actually present. No tests of specific screen readers were supplied, so no claim is made here about how any one of them announces a particular pattern. A spelled-out month and an explicit zone are less open to misreading than an all-numeric date. On web pages, the HTML time element can pair human-readable text with a machine-readable value in its datetime attribute. Whether a given assistive tool exposes that attribute should be tested, not assumed.

For search, sorting and editing, ISO 8601 strings in a single consistent offset sort chronologically as plain text. Strings with mixed offsets or localized patterns do not. Edit and store the instant, either as a timestamp or as an ISO string in UTC, and treat formatted text as output only. If the shared string has to support sorting, re-parsing or reliable machine use, include an ISO 8601 UTC value alongside the localized display.

Gaps in the available evidence

  • No performance or bundle-size measurements were supplied for date-fns, @date-fns/tz, @date-fns/utc or date-fns-tz.
  • No tested matrix of browsers, Node versions or ICU data builds was available, so outputs that depend on Intl, such as zone abbreviations or space characters, are not confirmed for any specific environment.
  • No screen-reader or other assistive-technology tests were supplied.
  • Package names, renamed functions and option names reflect documented behavior as generally known. Confirm them against the changelog of the versions you install.