@@ -7,9 +7,11 @@ import {
77} from "date-fns" ;
88import dayjs , { type Dayjs } from "dayjs" ;
99import utc from "dayjs/plugin/utc" ;
10+ import duration from "dayjs/plugin/duration" ;
1011import { formatDateRange } from "little-date" ;
1112
1213dayjs . extend ( utc ) ;
14+ dayjs . extend ( duration ) ;
1315
1416const shortenAmPm = ( text : string ) : string => {
1517 const shortened = ( text || "" ) . replace ( / A M / g, "am" ) . replace ( / P M / g, "pm" ) ;
@@ -133,3 +135,30 @@ export const formatDateAsUTC = (date: Dayjs): string => {
133135
134136 return `${ utcDate . format ( "MMM D" ) } , ${ timeStr } UTC` ;
135137} ;
138+
139+ /**
140+ * Formats a duration in milliseconds to a compact string like "1d2h30m".
141+ */
142+ export function formatDuration ( ms : number ) {
143+ const d = dayjs . duration ( ms ) ;
144+
145+ const years = Math . floor ( d . asYears ( ) ) ;
146+ const weeks = Math . floor ( d . asWeeks ( ) ) % 52 ;
147+ const days = d . days ( ) ;
148+ const hours = d . hours ( ) ;
149+ const minutes = d . minutes ( ) ;
150+ const seconds = d . seconds ( ) ;
151+ const milliseconds = d . milliseconds ( ) ;
152+
153+ let result = "" ;
154+
155+ if ( years > 0 ) result += `${ years } y` ;
156+ if ( weeks > 0 ) result += `${ weeks } w` ;
157+ if ( days > 0 ) result += `${ days } d` ;
158+ if ( hours > 0 ) result += `${ hours } h` ;
159+ if ( minutes > 0 ) result += `${ minutes } m` ;
160+ if ( seconds > 0 ) result += `${ seconds } s` ;
161+ if ( milliseconds > 0 ) result += `${ milliseconds } ms` ;
162+
163+ return result || "0ms" ;
164+ }
0 commit comments