Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/openstatusHQ/openstatus. ๐ซ Status page with uptime monitoring & API monitoring as code ๐ซ openstatus.dev
bun drizzle-orm monitoring monitoring-as-code nextjs observability on-call open-source shadcn-ui status-page statuspage synthetic-monitoring tinybird turso uptime uptime-checker uptime-monitor
Something went wrong. Try again.
2.8 kB ยท 105 lines
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106import { endOfDay, isSameDay, startOfDay } from "date-fns";
export function formatMilliseconds(ms: number) { if (ms > 1000) { return `${Intl.NumberFormat("en-US", { style: "unit", unit: "second", maximumFractionDigits: 2, }).format(ms / 1000)}`; }
return `${Intl.NumberFormat("en-US", { style: "unit", unit: "millisecond", }).format(ms)}`;}
export function formatPercentage(value: number, fractionDigits = 2) { if (Number.isNaN(value)) return "100%"; return `${Intl.NumberFormat("en-US", { style: "percent", minimumFractionDigits: fractionDigits, maximumFractionDigits: fractionDigits, }).format(value)}`;}
export function formatNumber(value: number) { return `${Intl.NumberFormat("en-US").format(value)}`;}
// TODO: think of supporting custom formats
export function formatDate(date: Date) { return date.toLocaleDateString("en-US", { year: "numeric", month: "long", day: "numeric", });}
export function formatDateTime(date: Date) { return date.toLocaleDateString("en-US", { month: "long", day: "numeric", hour: "numeric", minute: "numeric", });}
export function formatTime(date: Date) { return date.toLocaleTimeString("en-US", { hour: "numeric", minute: "numeric", });}
export function formatDateRange(from?: Date, to?: Date) { const sameDay = from && to && isSameDay(from, to); const isFromStartDay = from && startOfDay(from).getTime() === from.getTime(); const isToEndDay = to && endOfDay(to).getTime() === to.getTime();
if (sameDay) { if (from && to) { return `${formatDateTime(from)} - ${formatTime(to)}`; } }
if (from && to) { if (isFromStartDay && isToEndDay) { return `${formatDate(from)} - ${formatDate(to)}`; } return `${formatDateTime(from)} - ${formatDateTime(to)}`; }
if (to) { return `Until ${formatDateTime(to)}`; }
if (from) { return `Since ${formatDateTime(from)}`; }
return "All time";}
export function formatDistanceToNowShort(date: Date): string { const seconds = Math.max(0, Math.round((Date.now() - date.getTime()) / 1000)); if (seconds < 60) return `${seconds}s ago`; const minutes = Math.round(seconds / 60); if (minutes < 60) return `${minutes}m ago`; const hours = Math.round(minutes / 60); if (hours < 24) return `${hours}h ago`; const days = Math.round(hours / 24); return `${days}d ago`;}
export function formatDateForInput(date: Date): string { const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, "0"); const day = String(date.getDate()).padStart(2, "0"); const hours = String(date.getHours()).padStart(2, "0"); const minutes = String(date.getMinutes()).padStart(2, "0");
return `${year}-${month}-${day}T${hours}:${minutes}`;}