Date Calculation

I am trying to calculate the number of days between 2 dates using
Time.now - object.date where object.date is a date pulled from a
database. I want to display the age of the entry in the database in
days. I keep getting an error about not being able to convert a date
into a float. What am I doing wrong. If I use object.date -
object.date it seems to work but the answer is useless (0). I am doing
all of this in the view.

Justin

Justin K. wrote:

I am trying to calculate the number of days between 2 dates using
Time.now - object.date where object.date is a date pulled from a
database. I want to display the age of the entry in the database in
days. I keep getting an error about not being able to convert a date
into a float. What am I doing wrong. If I use object.date -
object.date it seems to work but the answer is useless (0). I am doing
all of this in the view.

Justin

A noobs guess:

Do you mean object.to_time instead of object.date?

Jim wrote:

Justin K. wrote:

I am trying to calculate the number of days between 2 dates using
Time.now - object.date where object.date is a date pulled from a
database. I want to display the age of the entry in the database in
days. I keep getting an error about not being able to convert a date
into a float. What am I doing wrong. If I use object.date -
object.date it seems to work but the answer is useless (0). I am doing
all of this in the view.

Justin

A noobs guess:

Do you mean object.to_time instead of object.date?

no, in object.date, date is the name of the date field of the class
“object”.

Justin K. wrote:

Jim wrote:

Justin K. wrote:

I am trying to calculate the number of days between 2 dates using
Time.now - object.date where object.date is a date pulled from a
database. I want to display the age of the entry in the database in
days. I keep getting an error about not being able to convert a date
into a float. What am I doing wrong. If I use object.date -
object.date it seems to work but the answer is useless (0). I am doing
all of this in the view.

Justin

A noobs guess:

Do you mean object.to_time instead of object.date?

no, in object.date, date is the name of the date field of the class
“object”.

What do you get if you put this in your view??

<%= debug(object) %>
<%= object.date.class %>

Sky Y. wrote:

Time is time. Date is date. You can try (Date.today - object.date).to_i

On 1/22/06, Kevin O. [email protected] wrote:

<%= debug(object) %>
<%= object.date.class %>


Posted via http://www.ruby-forum.com/.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Blog >>> http://spaces.msn.com/members/skyincookoo

$ ./script/console

“2006-1-12”.to_time
=> Thu Jan 12 00:00:00 UTC 2006

So in this case, date is Time.

Time is time. Date is date. You can try (Date.today - object.date).to_i

On 1/22/06, Kevin O. [email protected] wrote:

<%= debug(object) %>
<%= object.date.class %>


Posted via http://www.ruby-forum.com/.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Blog >>> http://spaces.msn.com/members/skyincookoo

No, cause you cast it to Time. If you want a Date:

“2006-1-12”.to_date
=> #<Date: 4907495/2,0,2299161>

Sky Y. wrote:

No, cause you cast it to Time. If you want a Date:

“2006-1-12”.to_date
=> #<Date: 4907495/2,0,2299161>

Thanks for the hint. I needed to cast Time.now to date and it worked
fine. I was trying to use a time - date and that didn’t work very well.
So:

Time.now.to_date - object.created_on = success!

Am Sonntag, den 22.01.2006, 15:41 +0100 schrieb Justin K.:

Time.now.to_date - object.created_on = success!

If object.created_on is a Date object why not use:

Date.today - object.created_on = classiness!

But if it is a Time object as provided by the Activerecord::Timestamp
Module you should use:

((Time.now - object.created_on) / (606024)).floor


Norman T.

http://blog.inlet-media.de