formatDate

Documentation de cbk-toolkit

formatDate

Formate une date avec Intl.DateTimeFormat — locale fr-FR par défaut, fallback "-" si invalide. Accepte Date | string | number | null.

Démo en direct

Défaut (court)

20/07/2026

formatDate("2026-07-20T14:30:00Z")

Avec heure

20/07/2026 14:30

formatDateTime(...)

Heure seule

14:30

formatTime(...)

Long

20 juillet 2026

{ dateStyle: "long" }

Tester la locale

20 juillet 2026

Cas limites

formatDate(null)-
formatDate("invalid")-
formatDate(1713000000000)13/04/2024
Code source — copier-coller

Basique

import { formatDate } from "@ramses1er/cbk-toolkit/client";

formatDate("2026-07-20T14:30:00Z"); // "20/07/2026"
formatDate(new Date()); // "20/07/2026"
formatDate(1713000000000); // "13/04/2024"
formatDate(null); // "-"
formatDate("invalid"); // "-"

Avec Intl options

import { formatDate, formatDateTime, formatTime } from "@ramses1er/cbk-toolkit/client";

// Long
formatDate(date, { dateStyle: "long" }); // "20 juillet 2026"
formatDate(date, { dateStyle: "full" }); // "lundi 20 juillet 2026"

// Avec heure
formatDate(date, { dateStyle: "short", timeStyle: "short" }); // "20/07/2026 14:30"
formatDateTime(date); // "20/07/2026 14:30" (raccourci)

// Heure seule
formatTime(date); // "14:30"

// Locale
formatDate(date, { locale: "en-GB", dateStyle: "long" }); // "20 July 2026"

// Fallback custom
formatDate(null, { fallback: "N/A" }); // "N/A"

// Dans un Table
const columns: Column<User>[] = [
  { header: "Dernier accès", render: (row) => formatDate(row.lastAccess) },
];
API de référence
FonctionSignatureDescription
formatDate(value, options?) => stringDate seule par défaut (fr-FR court)
formatDateTime(value, options?) => stringDate + heure (2-digit)
formatTime(value, options?) => stringHeure seule
ParamTypeDéfautDescription
valueDate | string | number | nullrequisValeur à formater — invalide → fallback
options.localestring"fr-FR"Locale Intl
options.fallbackstring"-"Retour si null/invalide
options.*Intl.DateTimeFormatOptions—dateStyle, timeStyle, weekday, hour, etc.
Comportement
  • Sans options → day:2-digit month:2-digit year:numeric→ "20/07/2026"
  • null / undefined / "" / Invalid Date → fallback
  • Délègue à Intl.DateTimeFormat(locale, options) — pas de parsing manuel
  • Utile dans Table : render: (row) => formatDate(row.createdAt)