Forum: Ruby on Rails Date Calculation

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.
2a84ec1a0c6e96a8d80bd9f569c80322?d=identicon&s=25 Justin Kay (jkay)
on 2006-01-22 03:25
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
7c3a0706ce454a5e7c405a63fdcd2c21?d=identicon&s=25 Jim (Guest)
on 2006-01-22 03:38
Justin Kay 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?
2a84ec1a0c6e96a8d80bd9f569c80322?d=identicon&s=25 Justin Kay (jkay)
on 2006-01-22 04:07
Jim wrote:
> Justin Kay 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".
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (olbrich)
on 2006-01-22 06:03
Justin Kay wrote:
> Jim wrote:
>> Justin Kay 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 %>
Cee38055ae36590c654c04c2d5cc2778?d=identicon&s=25 Sky Yin (cookoo)
on 2006-01-22 08:08
(Received via mailing list)
Time is time. Date is date. You can try (Date.today - object.date).to_i

On 1/22/06, Kevin Olbrich <kevin.olbrich@duke.edu> wrote:
> >>>
>
> <%= debug(object) %>
> <%= object.date.class %>
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
Blog >>> http://spaces.msn.com/members/skyincookoo
7c3a0706ce454a5e7c405a63fdcd2c21?d=identicon&s=25 Jim (Guest)
on 2006-01-22 10:56
Sky Yin wrote:
> Time is time. Date is date. You can try (Date.today - object.date).to_i
>
> On 1/22/06, Kevin Olbrich <kevin.olbrich@duke.edu> wrote:
>> >>>
>>
>> <%= debug(object) %>
>> <%= object.date.class %>
>>
>> --
>> Posted via http://www.ruby-forum.com/.
>> _______________________________________________
>> Rails mailing list
>> Rails@lists.rubyonrails.org
>> 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.
Cee38055ae36590c654c04c2d5cc2778?d=identicon&s=25 Sky Yin (cookoo)
on 2006-01-22 12:34
(Received via mailing list)
No, cause you cast it to Time. If you want a Date:

>> "2006-1-12".to_date
=> #<Date: 4907495/2,0,2299161>
2a84ec1a0c6e96a8d80bd9f569c80322?d=identicon&s=25 Justin Kay (jkay)
on 2006-01-22 15:41
Sky Yin 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!
Bf66e10c8fc4abefebde0425e7f6f15a?d=identicon&s=25 Norman Timmler (Guest)
on 2006-01-23 09:09
(Received via mailing list)
Am Sonntag, den 22.01.2006, 15:41 +0100 schrieb Justin Kay:

> 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) / (60*60*24)).floor

--
Norman Timmler

http://blog.inlet-media.de
This topic is locked and can not be replied to.