# Date math without the dates

**URL:** <https://www.ruby-forum.com/t/date-math-without-the-dates/92000>\
**Category:** Ruby\
**Created:** [April 14, 2007, 7:20am UTC](https://www.ruby-forum.com/t/date-math-without-the-dates/92000 "2007-04-14T07:20:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [April 14, 2007, 7:20am UTC](https://www.ruby-forum.com/t/date-math-without-the-dates/92000/1 "2007-04-14T07:20:08Z")

</div>

Hi all. Observe:

(Date.today…Date.today + 90).to\_a.size # ~90

That operation is, unfortunately, slow. Well, when you’re calling it  
100+ times it’s slow. 🙂

Is there a way to get at the result of that operation (an integer) with  
two date objects? e.g.

class AccountTransaction

attr\_accessor :posted

def day\_count(date)  
(posted…date).to\_a.size  
end

end

Any ideas?

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [April 14, 2007, 7:23am UTC](https://www.ruby-forum.com/t/date-math-without-the-dates/92000/2 "2007-04-14T07:23:29Z")

</div>

Ack, some amendments…

Is there _another_ way to get at the result of that operation (an  
integer) besides the one I posted above?

And, I suppose a better title would have been “Faster date math.”

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [April 14, 2007, 7:40am UTC](https://www.ruby-forum.com/t/date-math-without-the-dates/92000/3 "2007-04-14T07:40:56Z")

</div>

On Apr 13, 11:23 pm, Daniel W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Ack, some amendments…
> 
> Is there _another_ way to get at the result of that operation (an  
> integer) besides the one I posted above?
> 
> And, I suppose a better title would have been “Faster date math.”
> 
> –  
> Posted viahttp://www.ruby-forum.com/.

irb(main):001:0\> ((Date.today + 90) - Date.today).to\_i  
=\> 90

Subtracting Dates from each other returns a Rational.

irb(main):002:0\> (Date.today + 90) - Date.today  
=\> Rational(90, 1)

You can just coerce that into an integer. No intermediate Array  
required.

HTH,  
Chris

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [April 14, 2007, 7:45am UTC](https://www.ruby-forum.com/t/date-math-without-the-dates/92000/4 "2007-04-14T07:45:29Z")

</div>

Chris S. wrote:

> You can just coerce that into an integer. No intermediate Array  
> required.

Ah, sweet! Crazy, too: I _just_ tried subtracting dates and found that a  
Rational was returned, but didn’t know you could coerce it into an  
integer. Thanks so much Chris! (And yes, it’s _much_ faster!)
