Sha256: fbf3f4f9ef38c75852cd0aac01abc60926763545c9bd728f8ba211e982cd4127

Contents?: true

Size: 1.98 KB

Versions: 1

Compression:

Stored size: 1.98 KB

Contents

module Account::DatesHelper
  def display_date(timestamp, custom_date_format = nil, format: :default, date_format: nil)
    return nil unless timestamp
    format = date_format if date_format

    if format && format == :default
      # e.g. October 11, 2018
      if custom_date_format
        local_time(timestamp).strftime(custom_date_format)
      elsif local_time(timestamp).year == local_time(Time.now).year
        local_time(timestamp).strftime("%B %-d")
      else
        local_time(timestamp).strftime("%B %-d, %Y")
      end
    else
      localize(local_time(timestamp).to_date, format: format)
    end
  end

  def display_time(timestamp, custom_time_format = nil, format: :default, time_format: nil)
    return nil unless timestamp
    format = time_format if time_format

    if format && format == :default
      # e.g. 4:22 PM
      local_time(timestamp).strftime(custom_time_format || "%l:%M %p")
    else
      localize(local_time(timestamp).to_time, format: format)
    end
  end

  def display_date_and_time(timestamp, custom_date_format = nil, custom_time_format = nil, format: :default, date_format: nil, time_format: nil)
    return nil unless timestamp
    format = "#{date_format} #{time_format}" if date_format && time_format

    if format && format == :default
      # e.g. Today at 4:22 PM
      # e.g. Yesterday at 2:12 PM
      # e.g. April 24 at 7:39 AM
      # today?
      if local_time(timestamp).to_date == local_time(Time.now).to_date
        "Today at #{display_time(timestamp, custom_time_format)}"
      # yesterday?
      elsif (local_time(timestamp).to_date) == (local_time(Time.now).to_date - 1.day)
        "Yesterday at #{display_time(timestamp, custom_time_format)}"
      else
        "#{display_date(timestamp, custom_date_format)} at #{display_time(timestamp, custom_time_format)}"
      end
    else
      localize(local_time(timestamp).to_datetime, format: format)
    end
  end

  def local_time(timestamp)
    timestamp&.in_time_zone(current_user.time_zone)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bullet_train-1.3.1 app/helpers/account/dates_helper.rb