diff --git a/src/utils.js b/src/utils.js index abb36c0..c5ba796 100644 --- a/src/utils.js +++ b/src/utils.js @@ -42,22 +42,25 @@ export const parse = text => markdown.render(text.replaceAll('<', '<').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` }