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
Understanding Method_Added for Class Methods
Passenger Installation with Nginx Fails
Ruby/Rails Image Processing Libraries
Checking Whether the C Compiler Works... No
Why Doesn't This Work If in Ruby Everything Is an Object
Start or Ensure That Delayed Job Runs When an Application/Server Restarts
Rails 3, Check CSV File Encoding Before Import
Refactoring a Large Routes.Rb File in Rails 4
Rails Implementation of Search with Autocomplete
Strictly Convert String to Integer (Or Nil)
Error Occurred While Installing Mini_Racer (0.2.0)
Why I Can Not Call Super in Define_Method with Overloading Method
Ruby: How to Store and Display a Day of the Week
Heroku: No Rakefile Found (But Works Locally)