Background
Foreground
Primary
Primary FG
Formats and displays dates and times in human-readable styles.
---import { Date } from '@bejamas/ui/components/date';import { Separator } from '@bejamas/ui/components/separator';---
<div class="grid grid-cols-2 gap-4"> <div>Default:</div> <Date date="2025-06-13T12:00:00Z" /> <Separator class="col-span-2" /> <div>Short:</div> <Date date="2025-06-13T12:00:00Z" format="short" /> <Separator class="col-span-2" /> <div>Time:</div> <Date date="2025-06-13T12:00:00Z" format="time" /> <Separator class="col-span-2" /> <div>Full:</div> <Date date="2025-06-13T12:00:00Z" format="full" /> <Separator class="col-span-2" /> <div>Long:</div> <Date date="2025-06-13T12:00:00Z" format="long" /> <Separator class="col-span-2" /> <div>Long with time:</div> <Date date="2025-06-13T12:00:00Z" format="long" includeTime /></div> bunx bejamas add date npx bejamas add date pnpm dlx bejamas add date yarn dlx bejamas add date---/** * @component Date * @description Formats and displays dates and times in human-readable styles. * @figmaUrl https://www.figma.com/design/koxai7zw5vIuBzuVxe5T2l/bejamas-ui?node-id=2527-2986&t=YFStJ3V8fXEO8QD8-4 * * @preview * * <div class="grid grid-cols-2 gap-4"> * <div>Default:</div> <Date date="2025-06-13T12:00:00Z" /> * <Separator class="col-span-2" /> * <div>Short:</div> <Date date="2025-06-13T12:00:00Z" format="short" /> * <Separator class="col-span-2" /> * <div>Time:</div> <Date date="2025-06-13T12:00:00Z" format="time" /> * <Separator class="col-span-2" /> * <div>Full:</div> <Date date="2025-06-13T12:00:00Z" format="full" /> * <Separator class="col-span-2" /> * <div>Long:</div> <Date date="2025-06-13T12:00:00Z" format="long" /> * <Separator class="col-span-2" /> * <div>Long with time:</div> <Date date="2025-06-13T12:00:00Z" format="long" includeTime /> * </div> * * @usage * * ```astro * --- * import { Date } from '@bejamas/ui/components/date'; * --- * * <Date date="2025-06-24T12:00:00Z" /> * ``` * * @examples * * ### Custom locale * * <div class="flex gap-2 flex-col"> * <Date date="2025-06-24T12:00:00Z" locale="fr-FR" /> * <Date date="2025-06-24T12:00:00Z" locale="de-DE" /> * <Date date="2025-06-24T12:00:00Z" locale="es-ES" /> * <Date date="2025-06-24T12:00:00Z" locale="pl-PL" /> * </div> */const { date, format = "long", includeTime = false, locale = "en-US", class: className = "", ...props} = Astro.props;
// Helper: parse date inputfunction parseDate(input) { if (!input) return null; if (input instanceof Date) return input; // Try to parse string const d = new Date(input); return isNaN(d.getTime()) ? null : d;}
const parsedDate = parseDate(date);
// Precompute separate date and time strings to support custom rendering via slotlet dateText = "";let timeText = "";let formatted = "";let html = "";if (parsedDate) { let dateOptions; switch (format) { case "short": dateOptions = { dateStyle: "short" }; break; case "full": dateOptions = { dateStyle: "full" }; break; case "time": dateOptions = undefined; break; case "long": default: dateOptions = { dateStyle: "long" }; break; } if (dateOptions) { dateText = new Intl.DateTimeFormat(locale, dateOptions).format(parsedDate); } if (includeTime || format === "time") { timeText = new Intl.DateTimeFormat(locale, { timeStyle: "short" }).format( parsedDate, ); } formatted = format === "time" ? timeText : includeTime && timeText ? `${dateText}, ${timeText}` : dateText;
if (Astro.slots.has("default")) { html = await Astro.slots.render("default", [ { date: dateText, time: timeText, formatted, iso: parsedDate.toISOString(), dateObj: parsedDate, }, ]); }}---
{ parsedDate ? ( Astro.slots.has("default") ? ( <Fragment set:html={html} /> ) : ( <time dateTime={parsedDate.toISOString()} class={className} {...props}> {formatted} </time> ) ) : ( <span class={["text-destructive", className].join(" ")} {...props}> Invalid date </span> )}---import { Date } from '@bejamas/ui/components/date';---
<Date date="2025-06-24T12:00:00Z" />---import { Date } from '@bejamas/ui/components/date';---
<div class="flex gap-2 flex-col"> <Date date="2025-06-24T12:00:00Z" locale="fr-FR" /> <Date date="2025-06-24T12:00:00Z" locale="de-DE" /> <Date date="2025-06-24T12:00:00Z" locale="es-ES" /> <Date date="2025-06-24T12:00:00Z" locale="pl-PL" /></div>