Status Utilities
openstatus/status-utilsFreeUtility
Utility functions for status formatting and display
Install it
npx shadcn@latest add https://openstatus.dev/r/status-utils.jsonSource
1import { UTCDate } from "@date-fns/utc";2import type { StatusBlocksLabels } from "@/components/blocks/status-i18n";3import { endOfDay, isSameDay, startOfDay } from "date-fns";45/**6 * Formats a date range in a human-readable format.7 *8 * NOTE: While Intl.DateTimeFormat.formatRange() is available in modern browsers,9 * we use a custom implementation to have fine-grained control over the output format10 * and to handle edge cases like "Since", "Until", and "All time" consistently.11 * See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/formatRange12 *13 * @param from - Start date of the range (optional)14 * @param to - End date of the range (optional)15 * @param locale - Locale string for formatting (default: "en-US")16 * @returns A formatted date range string17 *18 * @example19 * formatDateRange(new Date('2024-01-01'), new Date('2024-01-05'))20 * // => "January 1, 2024 - January 5, 2024"21 *22 * @example23 * formatDateRange(new Date('2024-01-01 10:00'), new Date('2024-01-01 15:00'))24 * // => "January 1, 10:00 AM - 3:00 PM"25 */26export function formatDateRange(from?: Date, to?: Date) {27 const sameDay = from && to && isSameDay(new UTCDate(from), new UTCDate(to));28 const isFromStartDay =29 from && startOfDay(new UTCDate(from)).getTime() === from.getTime();30 const isToEndDay = to && endOfDay(new UTCDate(to)).getTime() === to.getTime();3132 if (sameDay) {33 if (from && to && from.getTime() === to.getTime()) {34 return formatDateTime(from);35 }36 if (from && to) {37 return `${formatDateTime(from)} - ${formatTime(to)}`;38 }39 }4041 if (from && to) {42 if (isFromStartDay && isToEndDay) {43 return `${formatDate(from)} - ${formatDate(to)}`;44 }45 return `${formatDateTime(from)} - ${formatDateTime(to)}`;46 }4748 if (to) {49 return `Until ${formatDateTime(to)}`;50 }5152 if (from) {53 return `Since ${formatDateTime(from)}`;54 }5556 return "All time";57}5859/**60 * Formats a date with locale support.61 *62 * @param date - The date to format63 * @param options - Intl.DateTimeFormatOptions to customize the output64 * @param locale - Locale string for formatting (default: "en-US")65 * @returns A formatted date string66 */67export function formatDate(68 date: Date,69 options?: Intl.DateTimeFormatOptions,70 locale = "en-US",71) {72 return date.toLocaleDateString(locale, {73 year: "numeric",74 month: "long",75 day: "numeric",76 ...options,77// … truncated
What it pulls in
npm packages
Files it writes
More from openstatus
All 26 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Status Typesstatus-types | openstatus | Utilities | Free | no deps |
