BlockDex

Components

Registries

Free blocks

Method

BlockDex

Components

Registries

Free blocks

Method

Open API

BlockDex

Components

Registries

Free blocks

Method

Open API

Components/openstatus/status-utils

Status Utilities

openstatus/status-utils
FreeUtility

Utility functions for status formatting and display

Install it

npx shadcn@latest add https://openstatus.dev/r/status-utils.json

Source

src/components/blocks/status.utils.tsopenstatus.dev/r/status-utils.json · first 6 KB
1import { UTCDate } from "@date-fns/utc";
2import type { StatusBlocksLabels } from "@/components/blocks/status-i18n";
3import { endOfDay, isSameDay, startOfDay } from "date-fns";
4
5/**
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 format
10 * 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/formatRange
12 *
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 string
17 *
18 * @example
19 * formatDateRange(new Date('2024-01-01'), new Date('2024-01-05'))
20 * // => "January 1, 2024 - January 5, 2024"
21 *
22 * @example
23 * 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();
31
32 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 }
40
41 if (from && to) {
42 if (isFromStartDay && isToEndDay) {
43 return `${formatDate(from)} - ${formatDate(to)}`;
44 }
45 return `${formatDateTime(from)} - ${formatDateTime(to)}`;
46 }
47
48 if (to) {
49 return `Until ${formatDateTime(to)}`;
50 }
51
52 if (from) {
53 return `Since ${formatDateTime(from)}`;
54 }
55
56 return "All time";
57}
58
59/**
60 * Formats a date with locale support.
61 *
62 * @param date - The date to format
63 * @param options - Intl.DateTimeFormatOptions to customize the output
64 * @param locale - Locale string for formatting (default: "en-US")
65 * @returns A formatted date string
66 */
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

  • date-fns

Files it writes

  • src/components/blocks/status.utils.ts

Facts

Registry
openstatus
Type
registry:lib
Access
Free
Files written
1
Theme wiring
ships cssVars
Licence
AGPL-3.0
First indexed
2026-08-01
Last confirmed
yesterday

Go to the source

openstatus.dev openstatusHQ/openstatus on GitHub Raw registry item This item as JSON

More from openstatus

All 26 items
Same registry, same kind — the ones you would have found next
ComponentRegistryKindAccessInstallsCommand
Status Typesstatus-typesopenstatusUtilitiesFreeno deps
BlockDex

Built by

Kynth Studio

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex

BlockDex

Built by

Kynth Studio

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex

BlockDex

Built by

Kynth Studio

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex