1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
def date(date, time = false) date = Date.parse(date) if date.is_a? String day = date.strftime('%d') hour = date.strftime('%I') if time case day when '01'..'09' then date_result = date.strftime('%B ') + day.gsub('0', '') + date.strftime(', %Y') else date_result = date.strftime('%B %d, %Y') end if time case hour when '01'..'09' then time_result = ' at ' + hour.gsub('0', '') + date.strftime(':%M %p') else time_result = ' at ' + date.strftime('%I:%M %p') end end unless time return date_result else return date_result + time_result end end
Refactorings
No refactoring yet !
Maciej Piechotka
July 5, 2008, July 05, 2008 13:10, permalink
May be something like that:
1 2 3 4 5
def date(date, time = false) date = Date.parse(date) if date.class.is_a? String date_str = date.strftime(time ? '%B %d, %Y %I:%M %p' : '%B %d, %Y') date_str.gsub(/ 0+/, ' ') end
Joe Grossberg
July 5, 2008, July 05, 2008 15:03, permalink
Without getting into better error handling (in case someone passes you an invalid date), this is the initial refactoring I'd do:
1 2 3 4 5 6 7
def date(date, time = false) return "" unless date.class.is_a? String date = Date.parse(date) date_result = date.strftime('%B %d, %Y') date_result += date.strftime('at %I:%M %p') if time date_result.gsub(' 0', ' ') end
mvanholstyn
July 5, 2008, July 05, 2008 15:57, permalink
1 2 3 4 5
def date(date, time = false) date = Date.parse(date) if date.class.is_a? String format = time ? "%B #{date.day}, %Y at #{date.strftime('%I').to_i}:%M %p" : "%B #{date.day}, %Y" date.strftime(format) end
scudco
July 5, 2008, July 05, 2008 21:48, permalink
Edit: Noticed that you had wanted no leading zeros for single digit dates and hours.
Your situation may call for this to all be in one method, but it seems like these should just be two separate methods each expressing what they do. This will help you avoid unreadable looking method calls like date(Time.now, true), where true is never revealing what it is there for. That's my thought, anyways. Good luck!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
require 'date' now = Time.now # Note that by calling date.to_s and always parsing you may lose some # performance(not really sure) by parsing it every time, but you won't # have to check the class of whatever was passed in. That is if date is a string # then calling to_s on it will produce itself. If date is a Date/DateTime # object then to_s will produce a parseable string. Either way you win. def date_with_time(date) DateTime.parse(date.to_s).strftime("%B %d, %Y at %I:%M %p").gsub(/ 0/,' ') end def date(date) Date.parse(date.to_s).strftime("%B %d, %Y").sub(/ 0/,' ') end puts date(now) puts date_with_time(now)
dcadenas.blogspot.com
July 6, 2008, July 06, 2008 10:12, permalink
I share the scudco point of view.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
def human_date date date = Date.parse(date) if date.is_a?(String) date.strftime date_format(date) end def human_date_with_time date date = Date.parse(date) if date.is_a?(String) date.strftime "#{date_format(date)} at #{time_format(date)}" end def date_format date "%B #{date.day}, %Y" end def time_format date "#{date.strftime('%I').to_i}:%M %p" end
Adam
August 15, 2008, August 15, 2008 15:55, permalink
ActiveSupport already does this, sort of. It doesn't include the exact time format you seek. However, you can extend DATE_FORMATS to include your own custom format.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
require "activesupport" Date::DATE_FORMATS[:human] = Date::DATE_FORMATS[:long] Time::DATE_FORMATS[:human] = lambda { |time| [ time.to_date.to_s(:human), time.strftime("%I:%M %p").sub(/^0/, "") ].join(" at ") } # Date.today.to_s(:human) # => "August 15, 2008" # # Time.now.to_s(:human) # => "August 15, 2008 at 11:53 AM" # # record.created_at.to_s(:human) # => "August 15, 2008 at 11:53 AM" # # record.created_at.to_date.to_s(:human) # => "August 15, 2008"
Seems like this method could use some re-factoring. Just a simple method which turns some kind of date format (such as a created_at column) into a "human date". E.g.: "July 5, 2008". If time is set to true, then it would return e.g.: "July 5, 2008 at 5:18 PM". Thanks!