# Date Calculation

**URL:** <https://www.ruby-forum.com/t/date-calculation/52975>\
**Category:** Rails\
**Created:** [January 22, 2006, 3:25am UTC](https://www.ruby-forum.com/t/date-calculation/52975 "2006-01-22T03:25:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jkay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b4bc9f/32.png) [@jkay](https://www.ruby-forum.com/u/jkay)\
**Post date:** [January 22, 2006, 3:25am UTC](https://www.ruby-forum.com/t/date-calculation/52975/1 "2006-01-22T03:25:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jkay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b4bc9f/32.png) [@jkay](https://www.ruby-forum.com/u/jkay)\
**Post date:** [January 22, 2006, 3:38am UTC](https://www.ruby-forum.com/t/date-calculation/52975/2 "2006-01-22T03:38:04Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jkay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b4bc9f/32.png) [@jkay](https://www.ruby-forum.com/u/jkay)\
**Post date:** [January 22, 2006, 4:07am UTC](https://www.ruby-forum.com/t/date-calculation/52975/3 "2006-01-22T04:07:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jkay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b4bc9f/32.png) [@jkay](https://www.ruby-forum.com/u/jkay)\
**Post date:** [January 22, 2006, 6:03am UTC](https://www.ruby-forum.com/t/date-calculation/52975/4 "2006-01-22T06:03:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jkay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b4bc9f/32.png) [@jkay](https://www.ruby-forum.com/u/jkay)\
**Post date:** [January 22, 2006, 10:56am UTC](https://www.ruby-forum.com/t/date-calculation/52975/5 "2006-01-22T10:56:20Z")

</div>

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](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > > >
> > 
> > \<%= debug(object) %\>  
> > \<%= object.date.class %\>
> > 
> > –  
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> > 
> > * * *
> > 
> > Rails mailing list  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> –  
> Blog \>\>\> [http://spaces.msn.com/members/skyincookoo](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.

---

<div class="post-metadata">

**Author:** ![jkay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b4bc9f/32.png) [@jkay](https://www.ruby-forum.com/u/jkay)\
**Post date:** [January 22, 2006, 8:08am UTC](https://www.ruby-forum.com/t/date-calculation/52975/6 "2006-01-22T08:08:48Z")

</div>

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](mailto:removed_email_address@domain.invalid) wrote:

> > > >
> 
> \<%= debug(object) %\>  
> \<%= object.date.class %\>
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Blog \>\>\> [http://spaces.msn.com/members/skyincookoo](http://spaces.msn.com/members/skyincookoo)

---

<div class="post-metadata">

**Author:** ![jkay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b4bc9f/32.png) [@jkay](https://www.ruby-forum.com/u/jkay)\
**Post date:** [January 22, 2006, 12:34pm UTC](https://www.ruby-forum.com/t/date-calculation/52975/7 "2006-01-22T12:34:09Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![jkay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b4bc9f/32.png) [@jkay](https://www.ruby-forum.com/u/jkay)\
**Post date:** [January 22, 2006, 3:41pm UTC](https://www.ruby-forum.com/t/date-calculation/52975/8 "2006-01-22T15:41:07Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![jkay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b4bc9f/32.png) [@jkay](https://www.ruby-forum.com/u/jkay)\
**Post date:** [January 23, 2006, 9:09am UTC](https://www.ruby-forum.com/t/date-calculation/52975/9 "2006-01-23T09:09:06Z")

</div>

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

–  
Norman T.

[http://blog.inlet-media.de](http://blog.inlet-media.de)
