Count number of days between two dates
How do I count the number of days between these two dates?
start_date = Date.parse "2012-03-02 14:46:21 +0100"
end_date = Date.parse "2012-04-02 14:46:21 +0200"
With the Date (and DateTime) classes you can do (end_date - start_date).to_i
to get the number of days difference.
Assuming that end_date
and start_date
are both of class ActiveSupport::TimeWithZone
in Rails, then you can use:
(end_date.to_date - start_date.to_date).to_i
Rails has some built in helpers that might solve this for you. One thing to keep in mind is that this is part of the Actionview Helpers, so they wont be available directly from the console.
Try this
<% start_time = "2012-03-02 14:46:21 +0100" %>
<% end_time = "2012-04-02 14:46:21 +0200" %>
<%= distance_of_time_in_words(start_time, end_time) %>
"about 1 month"