Module: Platformx::DateHelpers

Defined in:
lib/platformx/date.rb

Instance Method Summary collapse

Instance Method Details

#x_format_date(date: "", show_time: false, show_month: true) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/platformx/date.rb', line 50

def x_format_date(date: "", show_time: false, show_month: true)
    
    
    if date.nil?
        return ""
    else
        time = date.strftime("%A, %B %d, %Y")
    
        if show_time == true
            time = time + date.strftime(" %l:%M %p")
        end
        
        return time
    end
end

#x_format_datetime(date: "") ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/platformx/date.rb', line 66

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: "") ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/platformx/date.rb', line 76

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: "") ⇒ Object

Returns relative time in words referencing the given date relative_time_ago(Time.now) => ‘about a minute ago’



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

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: "") ⇒ Object



86
87
88
# File 'lib/platformx/date.rb', line 86

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

#x_time_in_words(date: "") ⇒ Object

Time in words



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

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