diff --git a/packages/core/useTimeAgo/index.ts b/packages/core/useTimeAgo/index.ts index 94967b9c96b..91165a30525 100644 --- a/packages/core/useTimeAgo/index.ts +++ b/packages/core/useTimeAgo/index.ts @@ -57,6 +57,13 @@ export interface UseTimeAgoOptions { * @default false */ showSecond?: boolean + + /** + * Rounding method to apply. + * + * @default 'round' + */ + rounding?: 'round' | 'ceil' | 'floor' } interface UseTimeAgoUnit { @@ -124,9 +131,11 @@ export function useTimeAgo(time: MaybeComputedRef, optio messages = DEFAULT_MESSAGES, fullDateFormatter = DEFAULT_FORMATTER, showSecond = false, + rounding = 'round', } = options - const { abs, round } = Math + const { abs } = Math + const roundFn = Math[rounding] const { now, ...controls } = useNow({ interval: updateInterval, controls: true }) function getTimeAgo(from: Date, now: Date) { @@ -160,7 +169,7 @@ export function useTimeAgo(time: MaybeComputedRef, optio } function format(diff: number, unit: UseTimeAgoUnit) { - const val = round(abs(diff) / unit.value) + const val = roundFn(abs(diff) / unit.value) const past = diff > 0 const str = applyFormat(unit.name, val, past)