문제

I have the following:

y1 = Date.parse("2008-02-01")
y2 = Date.today

I then want to

  1. subtract y2 - y1
  2. convert the result to some kind of number class
  3. divide the result by 365.25 and round the result up if it exceeds .5

My main question is point 2, what number class I should use? And that will probably answer point 3 I guess.

Or perhaps there is an even more efficient way of subtracting two dates and receive number of years, rounded up/down to the closest whole number?

도움이 되었습니까?

해결책

The result of subtraction is already a number (days as rational numbers).

((y2 - y1) / 365.25).round
# => 6

다른 팁

use can use

result = ((y2 - y1) / 365.25).round

this will give you the difference of number of days in integer. now you can check through ternary operator whether it is greater than .5 or not

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top