Sha256: 3d782870284d59b395f000510ac9da7763be009aac67cfe868fe56b487244913

Contents?: true

Size: 1.32 KB

Versions: 55

Compression:

Stored size: 1.32 KB

Contents

module Account::DatesHelper
  # e.g. October 11, 2018
  def display_date(timestamp, custom_date_format = nil)
    return nil unless timestamp
    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
  end

  # e.g. October 11, 2018 at 4:22 PM
  # e.g. Yesterday at 2:12 PM
  # e.g. April 24 at 7:39 AM
  def display_date_and_time(timestamp, custom_date_format = nil, custom_time_format = nil)
    return nil unless timestamp

    # 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
  end

  # e.g. 4:22 PM
  def display_time(timestamp, custom_time_format = nil)
    local_time(timestamp).strftime(custom_time_format || "%l:%M %p")
  end

  def local_time(time)
    return time if current_user.time_zone.nil?
    time.in_time_zone(current_user.time_zone)
  end
end

Version data entries

55 entries across 55 versions & 1 rubygems

Version Path
bullet_train-1.2.27 app/helpers/account/dates_helper.rb
bullet_train-1.2.26 app/helpers/account/dates_helper.rb
bullet_train-1.2.25 app/helpers/account/dates_helper.rb
bullet_train-1.2.24 app/helpers/account/dates_helper.rb
bullet_train-1.2.23 app/helpers/account/dates_helper.rb
bullet_train-1.2.22 app/helpers/account/dates_helper.rb
bullet_train-1.2.21 app/helpers/account/dates_helper.rb
bullet_train-1.2.20 app/helpers/account/dates_helper.rb
bullet_train-1.2.19 app/helpers/account/dates_helper.rb
bullet_train-1.2.18 app/helpers/account/dates_helper.rb
bullet_train-1.2.17 app/helpers/account/dates_helper.rb
bullet_train-1.2.16 app/helpers/account/dates_helper.rb
bullet_train-1.2.15 app/helpers/account/dates_helper.rb
bullet_train-1.2.14 app/helpers/account/dates_helper.rb
bullet_train-1.2.13 app/helpers/account/dates_helper.rb
bullet_train-1.2.12 app/helpers/account/dates_helper.rb
bullet_train-1.2.11 app/helpers/account/dates_helper.rb
bullet_train-1.2.10 app/helpers/account/dates_helper.rb
bullet_train-1.2.9 app/helpers/account/dates_helper.rb
bullet_train-1.2.8 app/helpers/account/dates_helper.rb