# Difference of two dates in rails

**URL:** <https://rubytalk.org/t/difference-of-two-dates-in-rails/63387>\
**Category:** ruby-talk\
**Created:** [19 August 2011 13:43 UTC](https://rubytalk.org/t/difference-of-two-dates-in-rails/63387 "2011-08-19T13:43:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kapil\_K](https://avatars.discourse-cdn.com/v4/letter/k/b487fb/32.png) [@Kapil\_K](https://rubytalk.org/u/Kapil_K)\
**Post date:** [19 August 2011 13:43 UTC](https://rubytalk.org/t/difference-of-two-dates-in-rails/63387/1 "2011-08-19T13:43:40Z")

</div>

How to get only days or months when two dates are differentiated?

if possible to get days or months using ruby? please give me the  
solution?

Thanks

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![11142](https://avatars.discourse-cdn.com/v4/letter/1/e0b2c6/32.png) [@11142](https://rubytalk.org/u/11142)\
**Post date:** [19 August 2011 15:51 UTC](https://rubytalk.org/t/difference-of-two-dates-in-rails/63387/2 "2011-08-19T15:51:31Z")

</div>

I don't think I understand you, but let's try...

irb(main):001:0\> t1 = Time.now  
=\> 2011-08-19 17:50:59 +0200  
irb(main):004:0\> t2 = Time.utc 2011, 1, 1  
=\> 2011-01-01 00:00:00 UTC

irb(main):005:0\> t1 - t2  
=\> 19929059.71875  
irb(main):006:0\> (t1 - t2) / (60\*60\*24)  
=\> 230.66041341145834

The above is number of days the dates differ. To get months, divide it  
once more by 30.

-- Matma Rex

> **···**
>
> 2011/8/19 Kapil K. \<kapil\_kumar\_517@yahoo.in\>:
> 
> > How to get only days or months when two dates are differentiated?
> > 
> > if possible to get days or months using ruby? please give me the  
> > solution?
> > 
> > Thanks
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
