07 / 07

How to format dates in next-intl?

next-intl provides two ways to format dates: using the useFormatter hook for dates outside messages, and embedding ICU syntax directly in translation strings for dates that are part of a message.

next-intl provides comprehensive date formatting that automatically adapts to the user's locale. You have two primary approaches depending on your use case: formatting standalone dates (like timestamps or labels) using the useFormatter hook, or embedding dates within translated messages using ICU syntax. The library is built on the native Intl.DateTimeFormat API and supports all standard options, ensuring consistent formatting across both client and server components.

For dates that are not part of a larger translated message (e.g., a timestamp in a UI widget), use the useFormatter hook. This hook returns a format object containing a dateTime method. The method accepts a Date object and formatting options that follow the standard Intl.DateTimeFormat API.

Basic date formatting with useFormatter
Formatting time with useFormatter
Using global formats with useFormatter

For dates that appear inside a larger sentence or phrase (e.g., "Ordered on {date}"), you should embed the date formatting directly in your translation message. This keeps all text for a sentence together, which is much easier for translators. Use the ICU syntax with the date keyword.

Message file with date placeholder
Using date in a component
ICU Date Format Options
  1. 1

    full: Full date format, e.g., "Tuesday, November 20, 2020"

  2. 2

    long: Long date format, e.g., "November 20, 2020"

  3. 3

    medium: Medium date format, e.g., "Nov 20, 2020"

  4. 4

    short: Short date format, e.g., "11/20/20"

  5. 5

    ::yyyyMMMd: Custom date skeleton for precise control, e.g., "Jul 9, 2024"

Custom date skeletons

next-intl also provides relativeTime for formatting dates relative to the current moment (e.g., "2 hours ago", "in 3 days"). This is available through the same useFormatter hook.

Relative time formatting
Relative time with update interval
Custom relative time unit

When formatting dates in Server Components, note that getFormatter is an asynchronous function and must be awaited. Forgetting await will result in a "is not a function" error.

Server Component date formatting
Best Practices for Date Formatting
  1. 1

    Use ISO 8601 strings (e.g., '2020-11-20T10:36:01.516Z') for date storage to ensure consistent parsing across time zones

  2. 2

    For dates inside messages, use ICU syntax so translators can adjust format according to locale conventions

  3. 3

    For UI elements where the date appears alone, use useFormatter for more flexibility

  4. 4

    Use useNow with updateInterval for relative times that need to refresh automatically

  5. 5

    In Server Components, always await getFormatter before using it

  6. 6

    For complex date manipulations (e.g., adding days, parsing), use libraries like date-fns alongside next-intl

Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.