Forum: Ruby on Rails Difference between two dates

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
51939e06ffa3cb79cb1bc30b40dbff58?d=identicon&s=25 John Paul (Guest)
on 2007-02-01 21: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 =

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.

B5feafb2c66fdff60029deda63f99f93?d=identicon&s=25 dweinand (Guest)
on 2007-02-01 22:35
(Received via mailing list)
Af9a9013100ddf2dbb0ca13487b60f8b?d=identicon&s=25 Ivor Paul (Guest)
on 2007-02-01 22:36
(Received via mailing list)

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.

3e60fc9bc7b3c1555324b2c7d1de17a9?d=identicon&s=25 Rob Olson (Guest)
on 2008-07-04 10:48
(Received via mailing list)
date_sent = Date.parse("2007-01-01")
date_now =

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 Paul <>
0e025e5f55bf75752ade494e0c76a505?d=identicon&s=25 Canuk (Guest)
on 2008-09-03 02:48
(Received via mailing list)
I have done something similar::

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

how_many_days_have_passed = (date_now - date_sent).to_i

Hope this helps!

On Feb 1 2007, 1:50 pm, John Paul <>
A74096209c78118ff69d50a51129d697?d=identicon&s=25 Jason Lillywhite (jlillywh)
on 2009-09-28 19: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.