Transform DateTime into simple Date in Ruby on Rails

collimarco picture collimarco · Jul 10, 2009 · Viewed 99.3k times · Source

I have a datetime column in db that I want to transform into a simple date when I show it to users.

How can I do that?

def shown_date
  # to_date does not exist, but is what I am looking for
  self.date || self.exif_date_time_original.to_date 
end

Answer

Ryan McGeary picture Ryan McGeary · Jul 10, 2009

DateTime#to_date does exist with ActiveSupport:

$ irb
>> DateTime.new.to_date
NoMethodError: undefined method 'to_date' for #<DateTime: -1/2,0,2299161>
    from (irb):1

>> require 'active_support/core_ext'
=> true

>> DateTime.new.to_date
=> Mon, 01 Jan -4712