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
None of the previous answers (to this date) gives the correct difference in days between two dates.
The one that comes closest is by thatdankent. A full answer would convert to_i and then divide:
(Time.now.to_i - 23.hours.ago.to_i) / 86400
>> 0
(Time.now.to_i - 25.hours.ago.to_i) / 86400
>> 1
(Time.now.to_i - 1.day.ago.to_i) / 86400
>> 1
In the question's specific example, one should not parse to Date if the time passed is relevant. Use Time.parse instead.