I changed the Toms response to suit my needs and can be used as a standalone module without turning it on anywhere. I placed the human_date.rb
file inside my lib
directory. I also fixed a difficult problem with calculating days_difference
.
module HumanDate include ActionView::Helpers::DateHelper extend self def date_distance(from, to, long_form = true) seconds_in_day = 60 * 60 * 24 days_difference = ((to.beginning_of_day.to_datetime - from.beginning_of_day.to_datetime)).floor case days_difference when -2 "day before yesterday" when -1 "yesterday" when 0 "today" when 1 "tomorrow" when 2..6 "this #{day_name(days_difference)}" when 7..13 "next #{to.strftime("%a")} (#{short_date(days_difference)})" else if days_difference > 0 "in #{distance_of_time_in_words(from, to)} (#{short_date(days_difference, long_form)})" else "#{distance_of_time_in_words(from, to)} ago (#{short_date(days_difference, long_form)})" end end end def short_date(this_many, include_day=false) format_string = "%-b %-d" format_string = "%a, #{format_string}" if include_day this_many.days.from_now.strftime(format_string) end def day_name(this_many) this_many.days.from_now.strftime("%A") end def days_left_in_month Time.now.end_of_month.day - Time.now.day end def days_left_in_week Time.now.end_of_week.day - Time.now.day end end
Using:
1.9.3-p392 :001 > HumanDate. date_distance(Time.now, 2.day.ago) => "day before yesterday" 1.9.3-p392 :002 > HumanDate. date_distance(Time.now, 3.days.from_now) => "this Tuesday" 1.9.3-p392 :003 > HumanDate. date_distance(Time.now, 12.days.from_now) => "next Thursday (Aug 1)" 1.9.3-p392 :004 > HumanDate. date_distance(Time.now, 122.days.from_now) => "in 4 months (Tue, Nov 19)" 1.9.3-p392 :005 > HumanDate. date_distance(Time.now, 122.days.ago) => "4 months ago (Wed, Mar 20)"
source share