Fix plurals in prettyDate()

This commit is contained in:
Christopher Gurnee 2021-12-20 08:33:22 -05:00
parent e98baf062a
commit 6193f452c3

View file

@ -42,22 +42,25 @@ export const parse = text => markdown.render(text.replaceAll('&lt;', '<').replac
// UTC to "Reddit time format" (e.g. 5 hours ago, just now, etc...)
export const prettyDate = createdUTC => {
const currentUTC = Math.floor((new Date()).getTime() / 1000)
const secondDiff = currentUTC - createdUTC
const dayDiff = Math.floor(secondDiff / 86400)
if (dayDiff < 0) return ''
if (dayDiff === 0) {
const secondDiff = currentUTC - createdUTC
if (secondDiff < 86400) {
if (secondDiff < 10) return 'just now'
if (secondDiff < 60) return `${secondDiff} seconds ago`
if (secondDiff < 120) return 'a minute ago'
if (secondDiff < 3600) return `${Math.floor(secondDiff / 60)} minutes ago`
if (secondDiff < 7200) return 'an hour ago'
if (secondDiff < 86400) return `${Math.floor(secondDiff / 3600)} hours ago`
return `${Math.floor(secondDiff / 3600)} hours ago`
}
if (dayDiff < 2) return `1 day ago`
const dayDiff = Math.floor(secondDiff / 86400)
if (dayDiff < 2) return '1 day ago'
if (dayDiff < 7) return `${dayDiff} days ago`
if (dayDiff < 14) return '1 week ago'
if (dayDiff < 31) return `${Math.floor(dayDiff / 7)} weeks ago`
if (dayDiff < 60) return '1 month ago'
if (dayDiff < 365) return `${Math.floor(dayDiff / 30)} months ago`
if (dayDiff < 730) return '1 year ago'
return `${Math.floor(dayDiff / 365)} years ago`
}