here is my code
what i am trying to achieve is the time similar to youtube/fb/instagram, show the posting time diff
like: 30s ago 5m ago 15h ago 6d ago 1w ago 5y ago
the code is working fine, but is to verbose, and i am wondering if some js "guru" can improve this using latest features of ecmascript
const timeAgo = () => {
const date1 = dayjs(Date.now())
const now = base.createdOn
const diffInSeconds = date1.diff(now, 'second', false)
let diff = date1.diff(now, 'second', false) + 's'
if (diffInSeconds > 60 * 60 * 24 * 7 * 30 * 12) {
return date1.diff(now, 'year', false) + 'y'
}
if (diffInSeconds > 60 * 60 * 24 * 7 * 30) {
return date1.diff(now, 'month', false) + 'm'
}
if (diffInSeconds > 60 * 60 * 24 * 7) {
return date1.diff(now, 'week', false) + 'w'
}
if (diffInSeconds > 60 * 60 * 24) {
return date1.diff(now, 'day', false) + 'd'
}
if (diffInSeconds > 60 * 60) {
return date1.diff(now, 'hour', false) + 'h'
}
if (diffInSeconds > 60) {
return date1.diff(now, 'minute', false) + 'm'
}
return diff
}