# Forum: Ruby Difference of 2 dates interms of years.

on 2013-02-22 20:39
```how can we get the experience years between two dates(ex:2012-01-11 to
2009-02-01)?

I need only years and not in months and days and if less than year
should get 0 .```
on 2013-02-22 20:41
`Post some things you've tried.`
on 2013-02-22 20:43
```Matt Mongeau wrote in post #1098484:
> Post some things you've tried.

I tried the below :

def calculate_difference
minutes = (Date.parse("2009-06-20") - Date.today).to_i / 60
days = minutes / (24*60)
minutes -= days * 24*60
hours = minutes / 60
minutes -= hours * 60
"#{days}d#{hours}h#{minutes}m"
end

which is not the one - I am looking for. :)```
on 2013-02-22 20:45
`How might you modify that to handle years?`
on 2013-02-22 20:48
```OK, now I'm just baffled...you are looking for years, so you post a
"thing you tried" which doesn't even reference years in the slightest.
Where did you get that code? Just a random post from somewhere or did
you write that gem yourself (no pun intended).

This is getting too obvious that you aren't here for "help" you're here
for programmers to work for you. You even went so far as to change your
name from "Love you Ruby" to this so people would help you again. It's
just pathetic.

-Ryan```
on 2013-02-22 20:53
```I have a coded solution to this problem, and I will share it with you if
I feel like you've made an effort.```
on 2013-02-22 21:32
```Am 22.02.2013 20:43, schrieb Xavier R.:
>    hours = minutes / 60
>    minutes -= hours * 60
>    "#{days}d#{hours}h#{minutes}m"
> end
>
> which is not the one - I am looking for. :)

Use irb to find out what each expression actually returns.

Consider this:

(Date.parse("2009-06-20") - Date.parse("2009-06-18")).to_i
=> 2```
on 2013-02-22 21:57
```Matt Mongeau wrote in post #1098486:
> How might you modify that to handle years?

I was actually trying to get the years from the below :

Yr = #{days}/ 365

but not working. And thus I am looking for another technique.```
on 2013-02-22 22:08
```On Feb 23, 2013 6:59 AM, "Xavier R." <lists@ruby-forum.com> wrote:
>
> Matt Mongeau wrote in post #1098486:
> > How might you modify that to handle years?
>
> I was actually trying to get the years from the below :
>
> Yr = #{days}/ 365
>
> but not working. And thus I am looking for another technique.

The technique is right; the details are wrong. Read the error message to
fix the syntax error, and look up floating point numbers if you need
more
precision.```
on 2013-02-22 22:11
```Matthew Kerwin wrote in post #1098503:
> On Feb 23, 2013 6:59 AM, "Xavier R." <lists@ruby-forum.com> wrote:
>>
>> Matt Mongeau wrote in post #1098486:
>> > How might you modify that to handle years?

Could you suggest any other technique please?

>
> The technique is right; the details are wrong. Read the error message to
> fix the syntax error, and look up floating point numbers if you need
> more
> precision.```
on 2013-02-22 23:18
```Xavier R. wrote in post #1098506:
> Could you suggest any other technique please?

Here's a strategy that might work:

* Lea el mensaje de error.
* エラーメッセージを読んでください。
* Прочитайте сообщение об ошибке.
* Basahin ang mga mensahe ng error.
* 阅读错误信息。
* Lue virheilmoitus.
دفعات بازدید: پیام خطا *
* Baca pesan kesalahan.
* Διαβάστε το μήνυμα σφάλματος.

(Apologies to native speakers if I got anything wrong in the above, no
offence intended.)

You said:

> Yr = #{days}/ 365
>
> but not working.

How "not working"?

Is there an error message?  Can you read the message?  Can you
copy-and-paste the error message into Google and see what you can
discover by yourself?  Can you look up basic Ruby syntax and work out
what's wrong with the line you posted?  If required, can you
copy-and-paste relevant messages or results into translate.google.com to
try and elucidate meaning therefrom?

Is it functioning, but producing a result other than what you expected?
Can you dig a bit deeper, using hints provided by some of us in earlier
posts to explore what your program is doing at every step?  And maybe
work out the point at which it deviates from your expectation?

On this issue I will say no more.```
on 2013-02-23 21:44
```Matthew Kerwin wrote in post #1098503:
> On Feb 23, 2013 6:59 AM, "Xavier R." <lists@ruby-forum.com> wrote:
>>
>> Matt Mongeau wrote in post #1098486:
>> > How might you modify that to handle years?

Any other technique could you suggest for the same?

>
> The technique is right; the details are wrong. Read the error message to
> fix the syntax error, and look up floating point numbers if you need
> more
> precision.```
on 2013-02-24 02:38
```Xavier why don't you try helping other instead of asking a multitude of
random questions?```