Incerteza
Incerteza

Reputation: 34884

Difference between 2 dates in days

I have 2 dates and difference between them can be over a month. I want to find a difference between them in day. However, b.days - a.days turns a blind eye to to the months and, possibly, years too.

require 'date'

a = Date.parse("20141030")
b = Date.parse("20141230")

b.day - a.day #=> 0

What's the easier way to find such a difference?

Upvotes: 0

Views: 106

Answers (1)

falsetru
falsetru

Reputation: 369134

Just subtract the one from the other:

(b - a)
# => (61/1)
(b - a).to_i
# => 61

The reason you got 0 is b.day and a.day returns day of the month: 30. (30 - 30 = 0)

b.day
# => 30
a.day
# => 30

Upvotes: 3

Related Questions