All files / core/lib date.ts

90% Statements 9/10
100% Branches 4/4
100% Functions 1/1
88.88% Lines 8/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25    2x 12x   12x   3x               3x 12x 12x 3x            
import i18n from './i18n'
 
export const timeAgo = (date: string | Date) => {
    const seconds = Math.floor((new Date().getTime() - new Date(date).getTime()) / 1000)
 
    if (seconds < 60) return i18n.t('date.just_now')
 
    const intervals = [
        { label: 'years_ago', seconds: 31536000 },
        { label: 'months_ago', seconds: 2592000 },
        { label: 'days_ago', seconds: 86400 },
        { label: 'hours_ago', seconds: 3600 },
        { label: 'minutes_ago', seconds: 60 },
    ]
 
    for (const interval of intervals) {
        const count = Math.floor(seconds / interval.seconds)
        if (count >= 1) {
            return i18n.t(`date.${interval.label}`, { count })
        }
    }
 
    return i18n.t('date.seconds_ago', { count: Math.floor(seconds) })
}