useDateFormatter

useDateFormatter is a hook providing localized date for the current locale. It is built around the native Intl.DateTimeFormat API with predefined date formats but can also be used using the same API.

See the MDN docs for full details on how to use the API.

useDateFormatter is built on top of the function dateFormatter. This function can also be used to format a date.

Format Output

SHORTMEDIUMLONG_WITH_TIMECUSTOM
en-US6/23/25Jun 23, 2025June 23, 2025 at 11:45 AM06/23
en-UK23/06/202523 Jun 202523 June 2025 at 11:4523/06
fr-FR23/06/202523 juin 202523 juin 2025 à 11:4523/06
it-IT23/06/2523 giu 202523 giugno 2025 alle ore 11:4523/06
de-DE23.06.2523.06.202523. Juni 2025 um 11:4523.06.
es-ES23/6/2523 jun 202523 de junio de 2025, 11:4523/6

Example

import { useDateFormatter, DATE_FORMAT } from "@dev-spendesk/grapes";

export default function ProductPage() {
  const dateFormatter = useDateFormatter();

  return <p>{dateFormatter(new Date(), DATE_FORMAT.MEDIUM)}</p>;
}

Example with Intl.DateTimeFormat options

import { useDateFormatter, DATE_FORMAT } from "@dev-spendesk/grapes";

export default function ProductPage() {
  const dateFormatter = useDateFormatter();

  return (
    <p>
      {dateFormatter(new Date(), DATE_FORMAT.CUSTOM, {
        day: "2-digit",
        month: "2-digit",
      })}
    </p>
  );
}

Example using only dateFormatter

import { dateFormatter } from "@dev-spendesk/grapes";

export function localeFormat(locale) {
  const formattedDate = dateFormatter(locale, new Date(), DATE_FORMAT.MEDIUM);
  return formattedDate;
}