-
-
Save ntim/8f28f986dacd89cdf0ee to your computer and use it in GitHub Desktop.
Rails helper time_ago_in_words() and distance_of_time_in_words() translated into an AngularJs filter in CoffeScript
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
| app.filter('distance_of_time_in_words', -> | |
| (from_time, to_time, include_seconds) -> | |
| include_seconds = include_seconds || true | |
| to_time = to_time || 0 | |
| distance_in_minutes = Math.round(Math.abs(to_time - from_time) / 60 / 1000) | |
| distance_in_seconds = Math.round(Math.abs(to_time - from_time) / 1000) | |
| if 0 <= distance_in_minutes and distance_in_minutes <= 1 | |
| if !include_seconds | |
| if distance_in_minutes == 0 | |
| return 'less than 1 minute ago' | |
| else | |
| return '' + distance_in_minutes + ' minutes ago' | |
| if 0 <= distance_in_seconds and distance_in_seconds <= 4 | |
| 'less than 5 seconds ago' | |
| else if 5 <= distance_in_seconds and distance_in_seconds <= 9 | |
| 'less than 10 seconds ago' | |
| else if 10 <= distance_in_seconds and distance_in_seconds <= 19 | |
| 'less than 20 seconds ago' | |
| else if 20 <= distance_in_seconds and distance_in_seconds <= 39 | |
| 'less than half a minute ago' | |
| else if 40 <= distance_in_seconds and distance_in_seconds <= 59 | |
| 'less than 1 minute ago' | |
| else | |
| '1 minute ago' | |
| else if 2 <= distance_in_minutes and distance_in_minutes <= 44 | |
| '' + distance_in_minutes + ' minutes ago' | |
| else if 45 <= distance_in_minutes and distance_in_minutes <= 89 | |
| 'about 1 hour ago' | |
| else if 90 <= distance_in_minutes and distance_in_minutes <= 1439 | |
| 'about ' + Math.round(distance_in_minutes / 60.0) + ' hours ago' | |
| else if 1440 <= distance_in_minutes and distance_in_minutes <= 2529 | |
| '1 day ago' | |
| else if 2530 <= distance_in_minutes and distance_in_minutes <= 43199 | |
| '' + Math.round(distance_in_minutes / 1440.0) + ' days ago' | |
| else if 43200 <= distance_in_minutes and distance_in_minutes <= 86399 | |
| 'about 1 month ago' | |
| else if 86400 <= distance_in_minutes and distance_in_minutes <= 525599 | |
| '' + Math.round(distance_in_minutes / 43200.0) + ' months ago' | |
| else | |
| distance_in_years = distance_in_minutes / 525600 | |
| minute_offset_for_leap_year = distance_in_years / 4 * 1440 | |
| remainder = (distance_in_minutes - minute_offset_for_leap_year) % 525600 | |
| if remainder < 131400 | |
| 'about ' + Math.round(distance_in_years) + ' years ago' | |
| else if remainder < 394200 | |
| 'over ' + Math.round(distance_in_years) + ' years ago' | |
| else | |
| 'almost ' + Math.round(distance_in_years + 1) + ' years ago' | |
| ) | |
| app.filter('time_ago_in_words', (distance_of_time_in_wordsFilter) -> | |
| (from_time, include_seconds) -> | |
| include_seconds = include_seconds || true | |
| distance_of_time_in_wordsFilter from_time, Date.now(), include_seconds | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment