BlockDex

Components

Registries

Free blocks

Method

BlockDex

Components

Registries

Free blocks

Method

Open API

BlockDex

Components

Registries

Free blocks

Method

Open API

Components/patchui/time-ago

Time Ago

patchui/time-ago
FreeComponent

patchui publishes no description for this item.

Install it

npx shadcn@latest add https://ui.hotfix.jobs/r/time-ago.json

Source

registry/components/ui/time-ago.tsxui.hotfix.jobs/r/time-ago.json · first 6 KB
1"use client";
2
3import { useEffect, useState, useSyncExternalStore } from "react";
4import type * as React from "react";
5import { cn } from "@/lib/utils";
6
7const defaultFallbackFormatOptions: Intl.DateTimeFormatOptions = {
8 month: "short",
9 day: "numeric",
10 timeZone: "UTC",
11};
12
13const defaultAbsoluteLabelFormatOptions: Intl.DateTimeFormatOptions = {
14 year: "numeric",
15 month: "short",
16 day: "numeric",
17 hour: "numeric",
18 minute: "2-digit",
19 timeZone: "UTC",
20 timeZoneName: "short",
21};
22
23const MIN = 60;
24const HOUR = 60 * 60;
25const DAY = 24 * 60 * 60;
26const WEEK = 7 * DAY;
27const MONTH = 30.4375 * DAY;
28const YEAR = 365.25 * DAY;
29const MAX_TIMEOUT = 2_147_000_000;
30
31function relativeUnitFor(seconds: number): {
32 divisor: number;
33 unit: Intl.RelativeTimeFormatUnit;
34} {
35 if (seconds >= YEAR) return { divisor: YEAR, unit: "year" };
36 if (seconds >= MONTH) return { divisor: MONTH, unit: "month" };
37 if (seconds >= WEEK) return { divisor: WEEK, unit: "week" };
38 if (seconds >= DAY) return { divisor: DAY, unit: "day" };
39 if (seconds >= HOUR) return { divisor: HOUR, unit: "hour" };
40 return { divisor: MIN, unit: "minute" };
41}
42
43function upperBoundaryFor(unit: Intl.RelativeTimeFormatUnit): number {
44 if (unit === "minute") return HOUR;
45 if (unit === "hour") return DAY;
46 if (unit === "day") return WEEK;
47 if (unit === "week") return MONTH;
48 if (unit === "month") return YEAR;
49 return Number.POSITIVE_INFINITY;
50}
51
52function formatTimeAgo(
53 dateMs: number,
54 formatter: Intl.RelativeTimeFormat,
55): string {
56 const deltaMs = dateMs - Date.now();
57 const signedSeconds = deltaMs / 1000;
58 const seconds = Math.abs(deltaMs) / 1000;
59
60 if (seconds < MIN) return formatter.format(0, "second");
61
62 const { divisor, unit } = relativeUnitFor(seconds);
63 return formatter.format(Math.trunc(signedSeconds / divisor), unit);
64}
65
66function nextRefreshDelay(dateMs: number): number | null {
67 if (!Number.isFinite(dateMs)) return null;
68
69 const signedSeconds = (dateMs - Date.now()) / 1000;
70 const seconds = Math.abs(signedSeconds);
71 let delaySeconds: number;
72
73 if (seconds < MIN) {
74 delaySeconds = signedSeconds > 0 ? seconds + MIN : MIN - seconds;
75 } else {
76 const { divisor, unit } = relativeUnitFor(seconds);
77 if (signedSeconds > 0) {
78 delaySeconds = seconds % divisor || 1;
79 } else {
80 const nextValue = (Math.floor(seconds / divisor) + 1) * divisor;
81 delaySeconds = Math.min(
82 nextValue - seconds,
83 upperBoundaryFor(unit) - seconds,
84 );
85 }
86// … truncated

What it pulls in

Other registry items

  • https://ui.hotfix.jobs/r/tokens.json
  • https://ui.hotfix.jobs/r/utils.json

Files it writes

  • registry/components/ui/time-ago.tsx

Facts

Registry
patchui
Type
registry:ui
Access
Free
Files written
1
Licence
MIT
First indexed
2026-08-02
Last confirmed
yesterday

Go to the source

ui.hotfix.jobs hotfix-jobs/patch-ui on GitHub Raw registry item This item as JSON

More from patchui

All 51 items
Same registry, same kind — the ones you would have found next
ComponentRegistryKindAccessInstallsCommand
AccordionaccordionpatchuiComponentsFree2 deps
AvataravatarpatchuiComponentsFree3 deps
BadgebadgepatchuiComponentsFree2 deps
BreadcrumbbreadcrumbpatchuiComponentsFree1 dep
ButtonbuttonpatchuiComponentsFree2 deps
CalendarcalendarpatchuiComponentsFree1 dep
CardcardpatchuiComponentsFree1 dep
CheckboxcheckboxpatchuiComponentsFree2 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