module Platformx::DateHelpers

Date helpers module @author Tim Mushen

Public Instance Methods

x_format_date(date: "", show_time: false, show_month: true, format: "long") click to toggle source

Formats the date @param date [Time] date representation in the format %A, %B %d, %Y @param show_time [Boolean] if the timme should be incldued @param show_month [Boolean] if teh month should be included @return [String] formated representation of the given date

# 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: "") click to toggle source

Format date time @param date [Time] the date-time to be formatted @return [String] the formatted date

# 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: "") click to toggle source

Format time @param time [Time] the time to be formatted @return [String] formatted time

# 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: "") click to toggle source

Returns relative time in words referencing the given date @param from_time [Time] the from time @return [String] a 'time ago' representation for the given time @example

relative_time_ago(Time.now) # => 'about a minute ago'
# 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: "") click to toggle source

Short date with format %A, %-m-%d-%y @param datevar [Time | Date] date or time to be formatted @return [String] formatted date/time

# 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: "") click to toggle source

Get the time in words @param date [String] a string representation of the date @return [String] time in words

# 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