1
0
Fork 0
freesewing/sites/shared/components/timeago/index.mjs

30 lines
1.2 KiB
JavaScript
Raw Normal View History

2023-08-16 15:54:32 +02:00
import { useTranslation } from 'next-i18next'
import { DateTime, Interval } from 'luxon'
import { capitalize } from 'shared/utils.mjs'
export const ns = ['timeago']
export const TimeAgo = ({ date }) => {
const { t } = useTranslation('timeago')
const i = Interval.fromDateTimes(DateTime.fromISO(date), DateTime.now())
.toDuration(['hours', 'days', 'months', 'years'])
.toObject()
let ago = ''
if (i.years < 1 && i.months < 1 && i.days < 1) {
if (Math.round(i.hours) === 1 || Math.floor(i.hours) === 1) ago += `${t('oneHour')}`
else if (Math.floor(i.hours) === 0) ago += `${t('lessThanAnHour')}`
else ago += `${Math.floor(i.hours)} ${t('hours')}`
} else if (i.years < 1 && i.months < 1) {
if (Math.floor(i.days) === 1) ago += `${t('oneDay')}`
else if (Math.floor(i.days) === 0) ago += `${t('lessThanADay')}`
else ago += `${i.days} ${t('days')}, ${Math.floor(i.hours)} ${t('hours')}`
2023-08-16 15:54:32 +02:00
} else {
if (i.years === 1) ago += `${i.years} ${t('year')}, `
else if (i.years > 1) ago += `${i.years} ${t('years')}, `
if (i.months === 1) ago += `${i.months} ${t('month')}`
else if (i.months > 1) ago += `${i.months} ${t('months')}`
}
return capitalize(`${ago} ${t('ago')}`)
}