How can I find the number of days between two Date objects in Ruby?

RubyDate

Ruby Problem Overview


How can I find the number of days between two Date objects?

Ruby Solutions


Solution 1 - Ruby

Subtract the beginning date from the end date:

endDate - beginDate 

Solution 2 - Ruby

irb(main):005:0> a = Date.parse("12/1/2010")
=> #<Date: 4911063/2,0,2299161>

irb(main):007:0> b = Date.parse("12/21/2010")
=> #<Date: 4911103/2,0,2299161>

irb(main):016:0> c = b.mjd - a.mjd
=> 20

This uses a Modified Julian Day Number.

From wikipedia:

> The Julian date (JD) is the interval of time in days and fractions of a day since January 1, 4713 BC Greenwich noon, Julian proleptic calendar.

Solution 3 - Ruby

This may have changed in Ruby 2.0

When I do this I get a fraction. For example on the console (either irb or rails c)

2.0.0-p195 :005 > require 'date'
 => true 
2.0.0-p195 :006 >  a_date = Date.parse("25/12/2013")
 => #<Date: 2013-12-25 ((2456652j,0s,0n),+0s,2299161j)> 
2.0.0-p195 :007 >  b_date = Date.parse("10/12/2013")
 => #<Date: 2013-12-10 ((2456637j,0s,0n),+0s,2299161j)> 
2.0.0-p195 :008 > a_date-b_date
 => (15/1) 

Of course, casting to an int give the expected result

2.0.0-p195 :009 > (a_date-b_date).to_i
 => 15 

This also works for DateTime objects, but you have to take into consideration seconds, such as this example

2.0.0-p195 :017 >   a_date_time = DateTime.now
 => #<DateTime: 2013-12-31T12:23:03-08:00 ((2456658j,73383s,725757000n),-28800s,2299161j)> 
2.0.0-p195 :018 > b_date_time = DateTime.now-20
 => #<DateTime: 2013-12-11T12:23:06-08:00 ((2456638j,73386s,69998000n),-28800s,2299161j)> 
2.0.0-p195 :019 > a_date_time - b_date_time
 => (1727997655759/86400000000) 
2.0.0-p195 :020 > (a_date_time - b_date_time).to_i
 => 19 
2.0.0-p195 :021 > c_date_time = a_date_time-20
 => #<DateTime: 2013-12-11T12:23:03-08:00 ((2456638j,73383s,725757000n),-28800s,2299161j)> 
2.0.0-p195 :022 > a_date_time - c_date_time
 => (20/1) 
2.0.0-p195 :023 > (a_date_time - c_date_time).to_i
 => 20 

Solution 4 - Ruby

In Ruby 2.1.3 things have changed:

> endDate = Date.new(2014, 1, 2)
 => #<Date: 2014-01-02 ((2456660j,0s,0n),+0s,2299161j)> 
> beginDate = Date.new(2014, 1, 1)
 => #<Date: 2014-01-01 ((2456659j,0s,0n),+0s,2299161j)> 
> days = endDate - beginDate
 => (1/1) 
> days.class
 => Rational 
> days.to_i
 => 1 

Solution 5 - Ruby

How about this?

(beginDate...endDate).count

The Range is a set of unique serials. And ... is an exclusive Range literal.

So beginDate..(endDate - 1) is same. Except is not.

In case when beginDate equals endDate, first element will be excluded because of uniqueness and ... will exclude last one. So if we want to .count dates between today and today it will return 0.

Solution 6 - Ruby

This worked for me:

(endDate - beginDate).to_i

Solution 7 - Ruby

Try this:

num_days = later_date - earlier_date

Solution 8 - Ruby

all of these steered me to the correct result, but I wound up doing

DateTime.now.mjd - DateTime.parse("01-01-1995").mjd

Solution 9 - Ruby

days = (endDate - beginDate)/(606024)

Solution 10 - Ruby

Well, take care of what you mean by "between" too...

days_apart = (to - from).to_i     # from + days_apart = to
total_days = (to - from).to_i + 1 # number of "selected" days
in_between_days = (to - from).to_i - 1 # how many days are in between from and to, i.e. excluding those two days

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrigView Question on Stackoverflow
Solution 1 - RubyAgile JediView Answer on Stackoverflow
Solution 2 - RubyRyanJMView Answer on Stackoverflow
Solution 3 - RubyJoe BasiricoView Answer on Stackoverflow
Solution 4 - Rubywieczorek1990View Answer on Stackoverflow
Solution 5 - RubympugachView Answer on Stackoverflow
Solution 6 - RubyHenok TView Answer on Stackoverflow
Solution 7 - RubyAndrew HareView Answer on Stackoverflow
Solution 8 - RubydabobertView Answer on Stackoverflow
Solution 9 - RubymichaelView Answer on Stackoverflow
Solution 10 - RubyestaniView Answer on Stackoverflow