@@ -25,6 +25,12 @@ export function formatAmount(amount: string | number, decimals = 7): string {
2525/** Format a date to a relative time string */
2626export function formatRelativeTime ( date : string | Date ) : string {
2727 const d = typeof date === "string" ? new Date ( date ) : date ;
28+
29+ // Handle invalid dates
30+ if ( isNaN ( d . getTime ( ) ) ) {
31+ return "Invalid date" ;
32+ }
33+
2834 const diff = Date . now ( ) - d . getTime ( ) ;
2935 const seconds = Math . floor ( diff / 1000 ) ;
3036 const minutes = Math . floor ( seconds / 60 ) ;
@@ -37,6 +43,62 @@ export function formatRelativeTime(date: string | Date): string {
3743 return "just now" ;
3844}
3945
46+ /** Format a date to absolute UTC timestamp */
47+ export function formatAbsoluteUTC ( date : string | Date ) : string {
48+ const d = typeof date === "string" ? new Date ( date ) : date ;
49+
50+ // Handle invalid dates
51+ if ( isNaN ( d . getTime ( ) ) ) {
52+ return "Invalid date" ;
53+ }
54+
55+ return d . toISOString ( ) . replace ( "T" , " " ) . substring ( 0 , 19 ) + " UTC" ;
56+ }
57+
58+ /** Format a date to absolute local timestamp */
59+ export function formatAbsoluteLocal ( date : string | Date ) : string {
60+ const d = typeof date === "string" ? new Date ( date ) : date ;
61+
62+ // Handle invalid dates
63+ if ( isNaN ( d . getTime ( ) ) ) {
64+ return "Invalid date" ;
65+ }
66+
67+ return new Intl . DateTimeFormat ( "en-US" , {
68+ year : "numeric" ,
69+ month : "short" ,
70+ day : "numeric" ,
71+ hour : "2-digit" ,
72+ minute : "2-digit" ,
73+ second : "2-digit" ,
74+ hour12 : false ,
75+ } ) . format ( d ) ;
76+ }
77+
78+ /** Format a date with explicit UTC/local behavior */
79+ export function formatTimestamp (
80+ date : string | Date ,
81+ options : {
82+ mode ?: "relative" | "absolute" ;
83+ timezone ?: "utc" | "local" ;
84+ } = { }
85+ ) : string {
86+ const { mode = "relative" , timezone = "local" } = options ;
87+
88+ const d = typeof date === "string" ? new Date ( date ) : date ;
89+
90+ // Handle invalid dates
91+ if ( isNaN ( d . getTime ( ) ) ) {
92+ return "Invalid date" ;
93+ }
94+
95+ if ( mode === "relative" ) {
96+ return formatRelativeTime ( d ) ;
97+ }
98+
99+ return timezone === "utc" ? formatAbsoluteUTC ( d ) : formatAbsoluteLocal ( d ) ;
100+ }
101+
40102/** Map chain name to its display color / class */
41103export const CHAIN_COLORS : Record < string , string > = {
42104 stellar : "text-chain-stellar" ,
0 commit comments