Converting Date Object to Timewithzone

Convert ActiveSupport::TimeWithZone to DateTime

DateTime is an old class which you generally want to avoid using. Time and Date are the two you want to be using. ActiveSupport::TimeWithZone acts like Time.

For stepping over dates you probably want to deal with Date objects. You can convert a Time (or ActiveSupport::TimeWithZone) into a Date with Time#to_date:

from.to_date.step(to.to_date, 7) { |d| puts d.to_s }

In ruby on rails, how could I convert a time in ActiveSupport::TimeWithZone to Month, Date, Year?

Use strftime like this:

 > t = Time.now
#=> 2015-05-11 12:47:32 +0530
> t.strftime('%m/%d/%Y')
#=> "05/11/2015"

Convert ActiveSupport::TimeWithZone class into String ActiveSupport::TimeWithZone

Nevermind, I figured out my issue. The error is occuring in the line return DateTime.parse(get_attri(object)) because I'm trying to parse an ActiveSupport::TimeWithZone object into a DateTime.



Related Topics



Leave a reply



Submit