Here is a quick algorithm for displaying time elapsed since a unix/epoch timestamp:
const showElapsedTime = (timestamp) => {
if (typeof timestamp !== 'number') return 'NaN'
const SECOND = 1000
const MINUTE = 1000 * 60
const HOUR = 1000 * 60 * 60
const DAY = 1000 * 60 * 60 * 24
const MONTH = 1000 * 60 * 60 * 24 * 30
const YEAR = 1000 * 60 * 60 * 24 * 30 * 12
const elapsed = ((new Date()).valueOf() - timestamp)
if (elapsed <= MINUTE) return `${Math.round(elapsed / SECOND)}s`
if (elapsed <= HOUR) return `${Math.round(elapsed / MINUTE)}m`
if (elapsed <= DAY) return `${Math.round(elapsed / HOUR)}h`
if (elapsed <= MONTH) return `${Math.round(elapsed / DAY)}d`
if (elapsed <= YEAR) return `${Math.round(elapsed / MONTH)}mo`
return `${Math.round(elapsed / YEAR)}y`
}
const createdAt = 1541301301000
console.log(showElapsedTime(createdAt + 5000000))
console.log(showElapsedTime(createdAt))
console.log(showElapsedTime(createdAt - 500000000))
(new Date()).valueOf() returns the number of seconds elapsed since Jan 1, 1970.
After you get that, you just need the timestamp of your event, and you can subtract it from the current time. This leaves the number of seconds elapsed, which can be converted into a human readable format by dividing by the correct number of units.
For example, 3000 milliseconds is 300 seconds. The algorithm I showed works with millisecond timestamps (divide everything by 1000 for seconds), so in the algorithm, 3000 would be be greater than MINUTE but less than HOUR, so it would return 3000 / MINUTE and return 3s.
This algorithm is useful if you are displaying a card, such as a job posting that was posted 2d ago.
I didn't like most of the other answers I found because they were not simple enough or readable enough. I hope my answer is quickly understandable.