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
SHORT | MEDIUM | LONG_WITH_TIME | CUSTOM | |
---|---|---|---|---|
en-US | 6/23/25 | Jun 23, 2025 | June 23, 2025 at 11:45 AM | 06/23 |
en-UK | 23/06/2025 | 23 Jun 2025 | 23 June 2025 at 11:45 | 23/06 |
fr-FR | 23/06/2025 | 23 juin 2025 | 23 juin 2025 à 11:45 | 23/06 |
it-IT | 23/06/25 | 23 giu 2025 | 23 giugno 2025 alle ore 11:45 | 23/06 |
de-DE | 23.06.25 | 23.06.2025 | 23. Juni 2025 um 11:45 | 23.06. |
es-ES | 23/6/25 | 23 jun 2025 | 23 de junio de 2025, 11:45 | 23/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;
}