質問

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