Skip to content

Instantly share code, notes, and snippets.

@mahemoff
Created January 23, 2012 17:56
Show Gist options
  • Select an option

  • Save mahemoff/1664524 to your computer and use it in GitHub Desktop.

Select an option

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"
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(' ', '&nbsp;')
.html_safe
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment