Last active
February 24, 2017 15:32
-
-
Save stigok/83a3e067a811e93f0303c1d0bef77621 to your computer and use it in GitHub Desktop.
Lightweight relative time ago in English
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const SECOND = 1000 | |
const MINUTE = SECOND * 60 | |
const HOUR = MINUTE * 60 | |
const DAY = HOUR * 24 | |
function timeAgo (date) { | |
const delta = Date.now() - parseInt(date) | |
const ago = { | |
day: delta / DAY, | |
hour: delta / HOUR, | |
minute: delta / MINUTE, | |
second: delta / SECOND | |
} | |
const keys = Object.keys(ago) | |
for (let i = 0; i < keys.length; i++) { | |
if (ago[keys[i]] > 1) { | |
return `${ago[keys[i]] | 0} ${keys[i]}${ago[keys[i]] >= 2 ? 's' : ''} ago` | |
} | |
} | |
return 'just now' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I wanted something lighter than moment.js, as I only wanted English language and strictly epoch formatted dates.