Date Calculation


#1

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


#2

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?


#3

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”.


#4

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 %>


#5

Sky Y. wrote:

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

On 1/22/06, Kevin O. removed_email_address@domain.invalid wrote:

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


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


Rails mailing list
removed_email_address@domain.invalid
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.


#6

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

On 1/22/06, Kevin O. removed_email_address@domain.invalid wrote:

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


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


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


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


#7

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

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


#8

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!


#9

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