# Day number delta

**URL:** <https://rubytalk.org/t/day-number-delta/26979>\
**Category:** ruby-talk\
**Created:** [27 April 2006 12:30 UTC](https://rubytalk.org/t/day-number-delta/26979 "2006-04-27T12:30:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lindsay\_Boyd](https://avatars.discourse-cdn.com/v4/letter/l/34f0e0/32.png) [@Lindsay\_Boyd](https://rubytalk.org/u/Lindsay_Boyd)\
**Post date:** [27 April 2006 12:30 UTC](https://rubytalk.org/t/day-number-delta/26979/1 "2006-04-27T12:30:17Z")

</div>

How can I calculate the number of days between two Date objects?. This  
is easy enough with Time objects using the epoch seconds method tv\_sec.  
Is there something similar for Dates?

thanks  
Lindsay

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Lindsay\_Boyd](https://avatars.discourse-cdn.com/v4/letter/l/34f0e0/32.png) [@Lindsay\_Boyd](https://rubytalk.org/u/Lindsay_Boyd)\
**Post date:** [27 April 2006 12:53 UTC](https://rubytalk.org/t/day-number-delta/26979/2 "2006-04-27T12:53:49Z")

</div>

Searching the web, I've seen this suggested:

&nbsp;&nbsp;(Date.parse(date2.to\_s) - Date.parse(date1.to\_s)).to\_i

but this strikes me as plain ugly! Surely there is a much better  
solution...

Lindsay

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Kirk\_Haines](https://avatars.discourse-cdn.com/v4/letter/k/c6cbf5/32.png) [@Kirk\_Haines](https://rubytalk.org/u/Kirk_Haines)\
**Post date:** [27 April 2006 13:20 UTC](https://rubytalk.org/t/day-number-delta/26979/3 "2006-04-27T13:20:23Z")

</div>

delta = a - b

Just subtract one from the other.

irb(main):001:0\> require 'date'  
=\> true  
irb(main):002:0\> a = Date.new(2006,4,28)  
=\> #\<Date: 4907707/2,0,2299161\>  
irb(main):003:0\> b = Date.new(2005,3,17)  
=\> #\<Date: 4906893/2,0,2299161\>  
irb(main):004:0\> c = a - b  
=\> Rational(407, 1)  
irb(main):005:0\> c.to\_i  
=\> 407

Kirk Haines

> **···**
>
> On Thursday 27 April 2006 6:30 am, Lindsay Boyd wrote:
> 
> > How can I calculate the number of days between two Date objects?. This  
> > is easy enough with Time objects using the epoch seconds method tv\_sec.  
> > Is there something similar for Dates?

---

<div class="post-metadata">

**Author:** ![Stefano\_Taschini](https://avatars.discourse-cdn.com/v4/letter/s/0ea827/32.png) [@Stefano\_Taschini](https://rubytalk.org/u/Stefano_Taschini)\
**Post date:** [27 April 2006 13:30 UTC](https://rubytalk.org/t/day-number-delta/26979/4 "2006-04-27T13:30:56Z")

</div>

You might want to require mathn as well:

require 'date'  
require 'mathn'

Date.new(2006,4,28) - Date.new(2005,3,17)  
&nbsp;&nbsp;&nbsp;&nbsp;#=\> 407

Ciao,  
Stefano

> **···**
>
> On 27/04/06, Kirk Haines \<khaines@enigo.com\> wrote:
> 
> > On Thursday 27 April 2006 6:30 am, Lindsay Boyd wrote:  
> > \> How can I calculate the number of days between two Date objects?. This  
> > \> is easy enough with Time objects using the epoch seconds method tv\_sec.  
> > \> Is there something similar for Dates?
> > 
> > delta = a - b
> > 
> > Just subtract one from the other.
> > 
> > irb(main):001:0\> require 'date'  
> > =\> true  
> > irb(main):002:0\> a = Date.new(2006,4,28)  
> > =\> #\<Date: 4907707/2,0,2299161\>  
> > irb(main):003:0\> b = Date.new(2005,3,17)  
> > =\> #\<Date: 4906893/2,0,2299161\>  
> > irb(main):004:0\> c = a - b  
> > =\> Rational(407, 1)  
> > irb(main):005:0\> c.to\_i  
> > =\> 407
> > 
> > Kirk Haines
