Difference between two dates


#1

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


#2

http://api.rubyonrails.com/classes/ActiveSupport/CoreExtensions/Numeric/Time.html
http://rubyforge.org/projects/chronic/
http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#M000572

HTH


#3

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


#4

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


#5

Rob Olson wrote:

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

why does it return a Rational type?


#6

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