Created
January 23, 2012 17:56
-
-
Save mahemoff/1664524 to your computer and use it in GitHub Desktop.
Smart date formatting - e.g. "27 minutes" "4 hours" "3 days, 15h" "4 months, 9d"
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
| def duration(from, to=Time.now) | |
| if to - from < 1.day | |
| distance_of_time_in_words(from,to,false,:precision=>1) | |
| else | |
| return distance_of_time_in_words(from,to,false,:precision=>2, :two_words_connector=>'@') | |
| .gsub(/@(\d+)\sseconds?/, ', \\1s') | |
| .gsub(/@(\d+)\sminutes?/, ', \\1m') | |
| .gsub(/@(\d+)\shours?/, ', \\1h') | |
| .gsub(/@(\d+)\sdays?/, ', \\1d') | |
| .gsub(/@(\d+)\sweeks?/, ', \\1w') | |
| .gsub(/@(\d+)\smonths?/, ', \\1m') | |
| .gsub(/@(\d+)\syears?/, ', \\1y') | |
| .gsub(' ', ' ') | |
| .html_safe | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment