The result of subtraction is already a number (days as rational numbers).
((y2 - y1) / 365.25).round
# => 6
質問
I have the following:
y1 = Date.parse("2008-02-01")
y2 = Date.today
I then want to
y2 - y1
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