forked from BRETT/BRETT
23 lines
922 B
TypeScript
23 lines
922 B
TypeScript
import { useNow } from '@vueuse/core'
|
|
|
|
// Auto-aktualisierende relative Zeit („vor 3 Min") — tickt alle 30s,
|
|
// damit offene Seiten ohne Reload aktuell bleiben
|
|
export function useTimeAgo() {
|
|
const now = useNow({ interval: 30_000 })
|
|
const { t } = useI18n()
|
|
|
|
return (value: string) => {
|
|
const seconds = Math.max(0, Math.floor((now.value.getTime() - new Date(value).getTime()) / 1000))
|
|
if (seconds < 60) return t('timeAgo.justNow')
|
|
const minutes = Math.floor(seconds / 60)
|
|
if (minutes < 60) return t('timeAgo.min', { n: minutes })
|
|
const hours = Math.floor(minutes / 60)
|
|
if (hours < 24) return t('timeAgo.hour', { n: hours })
|
|
const days = Math.floor(hours / 24)
|
|
if (days < 30) return t('timeAgo.day', { n: days })
|
|
const months = Math.floor(days / 30)
|
|
if (months < 12) return t('timeAgo.month', { n: months })
|
|
return t('timeAgo.year', { n: Math.floor(months / 12) })
|
|
}
|
|
}
|