time_difference icon indicating copy to clipboard operation
time_difference copied to clipboard

#in_months doesn't seem to respect months having different days

Open pedantic-git opened this issue 7 years ago • 4 comments

I'm trying to use TimeDifference to find the difference in months between two dates. I'd expect the difference between 2017-09-04 and 2017-12-04 to be exactly 3.0.

But this happens:

TimeDifference.between(Time.new(2017, 9, 4), Time.new(2017, 12, 4)).in_months
#=> 2.99
TimeDifference.between(Time.new(2017, 8, 4), Time.new(2017, 11, 4)).in_months
#=> 3.02

Would it make sense to add an #in_calendar_months or similar that takes into account the different lengths of different months?

pedantic-git avatar Dec 04 '17 13:12 pedantic-git

I've done a basic PR for this in #40 - let me know what you think!

pedantic-git avatar Dec 04 '17 13:12 pedantic-git

I am using this gem and it is having an issue around the leap day in 2020.

2.6.5 :007 > x = Chronic.parse('March 8').to_date 
 => Sun, 08 Mar 2020 
2.6.5 :008 > y = Chronic.parse('March 10').to_date 
 => Tue, 10 Mar 2020 
2.6.5 :009 > TimeDifference.between(x, y).in_days
 => 1.96 

I am not sure why it is less than 2.

martinstreicher avatar Oct 23 '19 00:10 martinstreicher

Hmm... DST?

martinstreicher avatar Oct 23 '19 00:10 martinstreicher

this is needed

TimeDifference.between(Time.now, 1.month.ago).in_months
0.99

I expected to be 1, but is based on 30days per month, any update on this?

joecabezas avatar Dec 18 '19 23:12 joecabezas