Module: Platformx::DateHelpers

Defined in:
lib/platformx/date.rb

Overview

Date helpers module

Author:

  • Tim Mushen

Instance Method Summary collapse

Instance Method Details

#x_format_date(date: "", show_time: false, show_month: true, format: "long") ⇒ String

Formats the date

Parameters:

  • date (Time) (defaults to: "")

    date representation in the format %A, %B %d, %Y

  • show_time (Boolean) (defaults to: false)

    if the timme should be incldued

  • show_month (Boolean) (defaults to: true)

    if teh month should be included

Returns:

  • (String)

    formated representation of the given date



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/platformx/date.rb', line 53

def x_format_date(date: "", show_time: false, show_month: true, format: "long")    
  if date.nil?
    return ""
  else


    if format == "mdy"
      time = date.strftime("%m-%d-%Y")
    elsif format == "d-m-y"
      time = date.strftime("%d-%m-%Y")
    elsif format == "m/d/y"
      time = date.strftime("%D")
    else
      # Long format
      time = date.strftime("%A, %B %d, %Y") 
    end 
    
    # Show time
    if show_time == true
      time = time + date.strftime(" %l:%M %p")
    end

    # Return time
    return time
  end
end

#x_format_datetime(date: "") ⇒ String

Format date time

Parameters:

  • date (Time) (defaults to: "")

    the date-time to be formatted

Returns:

  • (String)

    the formatted date



83
84
85
86
87
88
89
90
# File 'lib/platformx/date.rb', line 83

def x_format_datetime(date: "") 
  if date.nil?
    return ""
  else
    time = date.strftime("%A, %B %d, %Y %l:%M %p")
    return time
  end
end

#x_format_time(time: "") ⇒ String

Format time

Parameters:

  • time (Time) (defaults to: "")

    the time to be formatted

Returns:

  • (String)

    formatted time



95
96
97
98
99
100
101
102
# File 'lib/platformx/date.rb', line 95

def x_format_time(time: "")
  if time.nil?
    return ""
  else
    time = time.strftime("%l:%M %p")
    return time
  end
end

#x_relative_time_ago(from_time: "") ⇒ String

Returns relative time in words referencing the given date

Examples:

relative_time_ago(Time.now) # => 'about a minute ago'

Parameters:

  • from_time (Time) (defaults to: "")

    the from time

Returns:

  • (String)

    a ‘time ago’ representation for the given time



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/platformx/date.rb', line 30

def x_relative_time_ago(from_time: "")
  distance_in_minutes = (((Time.now - from_time.to_time).abs)/60).round

  case distance_in_minutes
  when 0..1 then 'A minute ago'
  when 2..44 then "#{distance_in_minutes} minutes ago"
  when 45..89 then '1 hour ago'
  when 90..1439 then "#{(distance_in_minutes.to_f / 60.0).round} hours ago"
  when 1440..2439 then '1 day ago'
  when 2440..2879 then '2 days ago'
  when 2880..43199 then "#{(distance_in_minutes / 1440).round} days ago"
  when 43200..86399 then 'About 1 month ago'
  when 86400..525599 then "#{(distance_in_minutes / 43200).round} months ago"
  when 525600..1051199 then 'About 1 year ago'
  else "Over #{(distance_in_minutes / 525600).round} years ago"
  end
end

#x_short_date(datevar: "") ⇒ String

Short date with format %A, %-m-%d-%y

Parameters:

  • datevar (Time | Date) (defaults to: "")

    date or time to be formatted

Returns:

  • (String)

    formatted date/time



107
108
109
# File 'lib/platformx/date.rb', line 107

def x_short_date(datevar: "")
  return datevar.strftime("%A, %-m-%d-%y") unless datevar.nil?
end

#x_time_in_words(date: "") ⇒ String

Get the time in words

Parameters:

  • date (String) (defaults to: "")

    a string representation of the date

Returns:

  • (String)

    time in words



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/platformx/date.rb', line 12

def x_time_in_words(date: "")
    date = date.to_date
    date = Date.parse(date, true) unless /Date.*/ =~ date.class.to_s
    days = (date - Date.today).to_i
    return 'Today' if days >= 0 and days < 1
    return 'Tomorrow' if days >= 1 and days < 2
    return 'Yesterday' if days >= -1 and days < 0
    return "In #{days} days" if days.abs < 60 and days > 0
    return "#{days.abs} days ago" if days.abs < 60 and days < 0
    return date.strftime('%A, %B %e, %Y') if days.abs < 182
    return date.strftime('%A, %B %e, %Y')
end