Add formatRelativeTime
function
#150
Replies: 3 comments 2 replies
-
Other functions to include: export function* timeUnits(): Generator<[number, Intl.RelativeTimeFormatUnit]> {
yield [31536000000, 'year'];
yield [2592000000, 'month'];
yield [86400000, 'day'];
yield [3600000, 'hour'];
yield [60000, 'minute'];
yield [1000, 'second'];
}
export function formatRelativeElapsedTime(ms: number, locale = 'en'): string {
for (const [factor, unit] of timeUnits()) {
if (Math.abs(ms) >= factor) {
const value = +(ms / factor).toFixed(1);
return new Intl.RelativeTimeFormat(locale, { numeric: 'auto' }).format(-value, unit);
}
}
return new Intl.RelativeTimeFormat(locale, { numeric: 'auto' }).format(0, 'second');
} |
Beta Was this translation helpful? Give feedback.
-
For me, it looks like too much sugar. BTW, there is no |
Beta Was this translation helpful? Give feedback.
-
Having something similar to vercel/ms could be interesting, the project appears to be no longer maintained and the latest version was published 3 years ago. Some fixes on the main branch have not been released for several month now. Here is some API examples: ms('2 days') // 172800000
ms('1d') // 86400000
ms('10h') // 36000000
ms('2.5 hrs') // 9000000 |
Beta Was this translation helpful? Give feedback.
-
I think Radashi could use a few tiny time-specific formatting functions.
The first one would be
formatRelativeTime
, which formats a given number and time unit into a locale-specific relative time string, such as "6 days ago". This function leverages theIntl.RelativeTimeFormat
API to ensure accurate and locale-aware formatting.n
: The numeric value representing the amount of time.unit
: The time unit as specified byIntl.RelativeTimeFormatUnit
(e.g., "day", "hour", "minute").locale
: Optional parameter for specifying the locale; defaults to "en" (English).Example Usage
Beta Was this translation helpful? Give feedback.
All reactions