module Platformx module DateHelpers ######################################################## # # Start Helpers # ######################################################## ######################################################## # # Time in words # ######################################################## 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 ######################################################## # # Returns relative time in words referencing the given date # relative_time_ago(Time.now) => 'about a minute ago' # ######################################################## 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 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 def x_format_datetime(date: "") if date.nil? return "" else time = date.strftime("%A, %B %d, %Y %l:%M %p") return time end end def x_format_time(time: "") if time.nil? return "" else time = time.strftime("%l:%M %p") return time end end def x_short_date(datevar: "") return datevar.strftime("%A, %-m-%d-%y") unless datevar.nil? end ######################################################## # # End # ######################################################## end end