first commit
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
export function formatRelativeFr(iso: string | number | Date): string {
|
||||
const d = typeof iso === 'string' || typeof iso === 'number' ? new Date(iso) : iso;
|
||||
if (!d || isNaN(d.getTime())) return '';
|
||||
const now = new Date();
|
||||
const diffMs = now.getTime() - d.getTime();
|
||||
const sec = Math.max(0, Math.floor(diffMs / 1000));
|
||||
const min = Math.floor(sec / 60);
|
||||
const hr = Math.floor(min / 60);
|
||||
const day = Math.floor(hr / 24);
|
||||
const week = Math.floor(day / 7);
|
||||
const month = Math.floor(day / 30);
|
||||
const year = Math.floor(day / 365);
|
||||
|
||||
if (year >= 1) return `il y a ${year} ${year > 1 ? 'ans' : 'an'}`;
|
||||
if (month >= 1) return `il y a ${month} ${month > 1 ? 'mois' : 'mois'}`;
|
||||
if (week >= 1) return `il y a ${week} ${week > 1 ? 'semaines' : 'semaine'}`;
|
||||
if (day >= 1) return `il y a ${day} ${day > 1 ? 'jours' : 'jour'}`;
|
||||
if (hr >= 1) return `il y a ${hr} ${hr > 1 ? 'heures' : 'heure'}`;
|
||||
if (min >= 1) return `il y a ${min} ${min > 1 ? 'minutes' : 'minute'}`;
|
||||
return `à l'instant`;
|
||||
}
|
||||
|
||||
export function formatAbsoluteFr(iso: string | number | Date): string {
|
||||
const d = typeof iso === 'string' || typeof iso === 'number' ? new Date(iso) : iso;
|
||||
if (!d || isNaN(d.getTime())) return '';
|
||||
// Exemple: 9 sept. 2025
|
||||
return new Intl.DateTimeFormat('fr-CA', { day: 'numeric', month: 'short', year: 'numeric' }).format(d);
|
||||
}
|
||||
|
||||
export function formatNumberFr(n: number): string {
|
||||
return new Intl.NumberFormat('fr-CA').format(n || 0);
|
||||
}
|
||||
Reference in New Issue
Block a user