Created
June 23, 2011 02:12
-
-
Save joshfrench/1041752 to your computer and use it in GitHub Desktop.
JS relative time
This file contains hidden or 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
Date::toRelativeTime = -> | |
delta = new Date() - this | |
intervals = | |
millisecond: 1 | |
second: 1000 | |
minute: 60 | |
hour: 60 | |
day: 24 | |
month: 30 | |
year: 12 | |
keys = (key for key of intervals) | |
delta = delta/intervals[interval = key] until delta < intervals[key = keys.shift()] | |
delta = Math.floor delta | |
interval += 's' if delta isnt 1 | |
"#{delta} #{interval} ago" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment