Forum: Ruby on Rails Difference between two dates

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
John P. (Guest)
on 2007-02-01 22:50
I am trying to subtract on date from the other to find how many days
have passed.

I want to know what I need to do to determine

date_sent = 2007-01-01
date_now = Date.today

I want to be able to subtract the date_sent from date_now and return the
accurate number of days that have passed.  Does anyone know how to do
this?  I also what to make sure it actually accounts for months being an
uneven number of days.

Thanks
JP
dweinand (Guest)
on 2007-02-01 23:35
(Received via mailing list)
Ivor P. (Guest)
on 2007-02-01 23:36
(Received via mailing list)
Hi

number_of_days = (date_now - date_sent).to_i

btw, didn't know this but, Data.parse("2007-01-01") returns the date

works great in the console.

regards
Ivor
Rob Olson (Guest)
on 2008-07-04 12:48
(Received via mailing list)
date_sent = Date.parse("2007-01-01")
date_now = Date.today

days_passed = (date_now - date_sent).to_i

Subtracting two dates gives a Rational which is the reason for the
integer conversion.

On Feb 1 2007, 1:50 pm, John P. <removed_email_address@domain.invalid>
Canuk (Guest)
on 2008-09-03 04:48
(Received via mailing list)
I have done something similar::

date_sent = Date.parse('2007-01-01')
date_now = Date.today

how_many_days_have_passed = (date_now - date_sent).to_i

Hope this helps!



On Feb 1 2007, 1:50 pm, John P. <removed_email_address@domain.invalid>
Jason L. (Guest)
on 2009-09-28 21:31
Rob Olson wrote:

> Subtracting two dates gives a Rational which is the reason for the
> integer conversion.

why does it return a Rational type?
This topic is locked and can not be replied to.