Add day ordinal suffix to Time : Time Extension « Time « Ruby






Add day ordinal suffix to Time


class Time
  def day_ordinal_suffix
    if day == 11 or day == 12
      return "th"
    else
      case day % 10
      when 1 then return "st"
      when 2 then return "nd"
      when 3 then return "rd"
      else return "th"
      end
    end
  end
end

time = Time.local(2008, 8, 31, 2, 3, 4, 5)

time.strftime("The %e#{time.day_ordinal_suffix} of %B") # => "The 31st of December"

 








Related examples in the same category

1.Add step method to Time
2.Validate a date