Module: TimeDateHelpers::DateHelpers

Defined in:
lib/time_date_helpers/date_helpers.rb

Instance Method Summary collapse

Instance Method Details

#convert_to_date(string) ⇒ Object

convert a string to a date (using Chronic, of course)



22
23
24
25
26
# File 'lib/time_date_helpers/date_helpers.rb', line 22

def convert_to_date(string)
  return nil if (string.nil? || string.class != String)
  tmp = Chronic.parse(string)
  tmp ? tmp.to_date : nil
end

#convert_to_datetime(string) ⇒ Object

convert a string to a datetime (Chronic’s default)



29
30
31
32
# File 'lib/time_date_helpers/date_helpers.rb', line 29

def convert_to_datetime(string)
  return nil if (string.nil? || string.class != String)
  Chronic.parse(string)
end

#humanize_date(date, opt = {}) ⇒ Object

convert a date to human format of mm/dd/yyyy



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/time_date_helpers/date_helpers.rb', line 4

def humanize_date(date, opt={})
  # Set the default options
  options = {:style => :calendar}
  # Merge whatever options the user has selected with the defaults
  options.merge!(opt)
  # Make sure what is passed is legit
  return nil if date.nil? 
  return nil unless date.class == Date || date.class == Time || date.class == DateTime
  if options[:style] == :calendar
    date.strftime("%m/%d/%Y")
  elsif options[:style] == :full
    date.day < 10 ? date.strftime("%B%e, %Y") : date.strftime("%B %e, %Y")
  else 
    nil
  end
end