All files / src/utils dateFormat.ts

100% Statements 37/37
100% Branches 30/30
100% Functions 4/4
100% Lines 29/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77                              35x   35x 35x 35x 35x     35x               1335x 1148x 963x 963x 177x     971x 1335x         607x 607x 476x         544x 544x 489x                   176x 176x   175x 175x 175x   9x 9x 4x 3x 2x    
/**
 * Date display helpers.
 *
 * Every date shown in the UI goes through these functions so that:
 * - the format follows the user's chosen UI language, not the browser's locale
 *   (a French UI in an en-US browser must not show "11/13/2026, 3:42:07 PM");
 * - text month styles are used ("13 nov. 2026"), which removes the day/month
 *   ambiguity of numeric formats like "03/04/2026";
 * - times are shown without seconds.
 *
 * Values are rendered in the viewer's local time zone.
 */
 
export type DateInput = string | number | Date | null | undefined
 
const PLAIN_DATE = /^(\d{4})-(\d{2})-(\d{2})$/
 
const SECOND_MS = 1000
const MINUTE_MS = 60 * SECOND_MS
const HOUR_MS = 60 * MINUTE_MS
const DAY_MS = 24 * HOUR_MS
 
/** Beyond this age, relative labels ("12 days ago") become less useful than the date itself. */
export const RELATIVE_MAX_AGE_MS = 7 * DAY_MS
 
/**
 * Parses a date value. A plain `YYYY-MM-DD` string (a date field's value) is a
 * calendar day, so it is read as local midnight: `new Date('2026-11-13')`
 * would read it as UTC midnight and show the previous day west of Greenwich.
 */
export function parseDate(value: DateInput): Date | null {
  if (value === null || value === undefined || value === '') return null
  if (typeof value === 'string') {
    const match = PLAIN_DATE.exec(value)
    if (match) {
      return new Date(Number(match[1]), Number(match[2]) - 1, Number(match[3]))
    }
  }
  const date = value instanceof Date ? value : new Date(value)
  return Number.isNaN(date.getTime()) ? null : date
}
 
/** "13 nov. 2026" / "Nov 13, 2026" — empty string when the value is missing or invalid. */
export function formatDate(value: DateInput, locale: string): string {
  const date = parseDate(value)
  if (!date) return ''
  return new Intl.DateTimeFormat(locale, { dateStyle: 'medium' }).format(date)
}
 
/** "13 nov. 2026, 15:42" / "Nov 13, 2026, 3:42 PM" — empty string when missing or invalid. */
export function formatDateTime(value: DateInput, locale: string): string {
  const date = parseDate(value)
  if (!date) return ''
  return new Intl.DateTimeFormat(locale, { dateStyle: 'medium', timeStyle: 'short' }).format(date)
}
 
/**
 * Relative label for recent social content: "à l'instant", "il y a 5 minutes",
 * "hier"... Falls back to the full date+time once older than
 * RELATIVE_MAX_AGE_MS, or for future dates (clock skew aside, a future
 * timestamp is not "recent").
 */
export function formatRelative(value: DateInput, locale: string, now: number = Date.now()): string {
  const date = parseDate(value)
  if (!date) return ''
  // A few seconds of client/server clock skew must not turn "just now" into a full date
  const ageMs = Math.max(0, now - date.getTime())
  const isFuture = date.getTime() - now > MINUTE_MS
  if (isFuture || ageMs >= RELATIVE_MAX_AGE_MS) return formatDateTime(date, locale)
 
  const rtf = new Intl.RelativeTimeFormat(locale, { numeric: 'auto' })
  if (ageMs < MINUTE_MS) return rtf.format(0, 'second')
  if (ageMs < HOUR_MS) return rtf.format(-Math.floor(ageMs / MINUTE_MS), 'minute')
  if (ageMs < DAY_MS) return rtf.format(-Math.floor(ageMs / HOUR_MS), 'hour')
  return rtf.format(-Math.floor(ageMs / DAY_MS), 'day')
}