How to Format Dates with date-fns

A practical guide to the date-fns format function: what it does, how format tokens work, escaping literal text, locales, and how to check your output.

date-fns is a JavaScript library of small, separate functions for working with dates. Its format function turns a date into text according to a pattern you choose. This guide explains what that function does to your data, how to build a pattern step by step, and how to check that the resulting text is the text you meant to produce.

What the format function does

The purpose of format is one-way conversion: it takes a moment in time and writes it out as a string. It does not change the date you pass in, and it does not read dates from text. Reading text into a date is the job of a separate function, parse, or parseISO for ISO 8601 strings such as 2026-01-05.

  • Input: a JavaScript Date object or a timestamp number. If your date starts as text, convert it to a Date first (for example with parseISO) rather than relying on format to interpret the string.
  • Pattern: a format string made of tokens, such as yyyy for a four-digit year or MM for a two-digit month.
  • Options (optional): settings such as locale, which controls month names, weekday names and localized date layouts.
  • Output: a plain string. Characters that are not tokens, such as hyphens, slashes and spaces, are copied into the output as they are, while letters are treated as tokens unless you escape them.

Scope matters here: format only controls how the date is written. It does not convert between time zones. It writes the date as it appears in the time zone of the environment running the code, such as the user's browser or your server. If you need output in a specific zone, that requires a separate time-zone add-on for date-fns, which is outside the scope of this guide.

Step 1: Import format and call it

The function takes the date first and the pattern second. The example below is illustrative; the comment shows the output the pattern is designed to produce for that date.

import { format } from 'date-fns';

// Example: January 5, 2026 (months are zero-based in the Date constructor)
const date = new Date(2026, 0, 5, 14, 30);

format(date, 'yyyy-MM-dd');
// expected: "2026-01-05"

Note the month argument in the Date constructor: 0 means January. That is a quirk of JavaScript's Date, not of date-fns, but it is a common source of output that is off by one month.

Step 2: Choose the right tokens

date-fns uses Unicode-style tokens, and they are case-sensitive. The same letter in a different case can mean something entirely different, so copy patterns exactly. The table lists commonly used tokens and what each one writes for the example date above (January 5, 2026, 14:30).

Token | Meaning | Example output
yyyy | Four-digit calendar year | 2026
yy | Two-digit year | 26
MM | Month, two digits | 01
M | Month, no padding | 1
MMM | Abbreviated month name | Jan
MMMM | Full month name | January
dd | Day of month, two digits | 05
d | Day of month, no padding | 5
EEE | Abbreviated weekday | Mon
EEEE | Full weekday | Monday
HH | Hour, 24-hour clock, two digits | 14
hh | Hour, 12-hour clock, two digits | 02
mm | Minutes, two digits | 30
ss | Seconds, two digits | 00
a | AM/PM marker | PM

Three case mix-ups cause most surprises:

  • MM is the month; mm is minutes. Writing yyyy-mm-dd puts minutes where the month should be.
  • yyyy is the calendar year; YYYY is the week-numbering year, which can differ from the calendar year around late December and early January.
  • dd is the day of the month; DD is the day of the year (for example, 005 or 365).

Because YYYY and D/DD are so often typed by mistake, date-fns refuses them by default and throws an error. If you genuinely need them, you must opt in through options named useAdditionalWeekYearTokens and useAdditionalDayOfYearTokens. In most cases the error is a sign you meant yyyy or dd instead.

Step 3: Add literal text safely

Any letter in the pattern may be read as a token, so words must be wrapped in single quotes to be copied as plain text. To produce an actual apostrophe in the output, write two single quotes in a row. Punctuation such as commas, hyphens, slashes and colons does not need quoting.

// Example patterns with literal text
format(date, "EEEE, MMMM d 'at' h:mm a");
// expected: "Monday, January 5 at 2:30 PM"

format(date, "'Report for' yyyy-MM-dd");
// expected: "Report for 2026-01-05"

format(date, "h 'o''clock'");
// expected: "2 o'clock"

If you forget the quotes, the letters inside a word are interpreted as tokens or rejected, so the output changes in ways that are easy to miss. When a pattern contains any words, quote them.

Step 4: Use a locale for other languages

Month and weekday names come out in English unless you pass a locale. Locales are imported separately and supplied through the options object. date-fns also provides localized layout tokens, P, PP, PPP and PPPP for dates and p for times, which let the locale decide the order and punctuation instead of hard-coding them.

import { format } from 'date-fns';
import { fr } from 'date-fns/locale';

// Example: French month name
format(date, 'd MMMM yyyy', { locale: fr });

// Example: locale-defined long date layout
format(date, 'PPPP', { locale: fr });

Exact output for localized layouts depends on the locale data shipped with your installed version, so these examples are shown without a fixed expected result. Check the actual output in your project before relying on it, especially if the text is stored or compared later.

Step 5: Verify the result

Formatting errors are often silent: the code runs, but the text is subtly wrong. A few checks catch most problems.

  • Test with a date where every field is different and unambiguous, such as day 5 in month 1 with time 14:30, so swapped or misread tokens stand out.
  • Include a single-digit day and month to confirm whether you wanted padding (dd, MM) or not (d, M).
  • Test a date in late December or early January if your pattern includes a year, to confirm you are using yyyy and not a week-year token.
  • Guard against invalid dates. Passing an Invalid Date (for example, the result of parsing malformed text) makes format throw a RangeError. Check with the isValid function before formatting if the input comes from users or files.
  • If the text will be read back later, parse it with the same pattern and confirm you get the same date. This round trip shows whether any information was lost, such as seconds or the time zone.
import { format, isValid, parseISO } from 'date-fns';

// Example: format text input only when it is a valid date
const input = '2026-01-05';
const parsed = parseISO(input);

const output = isValid(parsed) ? format(parsed, 'MMMM d, yyyy') : 'Invalid date';
// expected for this input: "January 5, 2026"

Gaps and version notes

No version-specific documentation or test results were supplied for this guide. The behavior described here, including case-sensitive tokens, quoted literals, locale options and the protection against YYYY and DD, reflects the long-standing documented behavior of format, but details such as which input types are accepted and exact localized output can vary between major versions. The example outputs are illustrative and were not run as part of writing this article; confirm them against the date-fns version installed in your project.