# Convert time to ruby time to calculate the time difference

**URL:** <https://rubytalk.org/t/convert-time-to-ruby-time-to-calculate-the-time-difference/60134>\
**Category:** ruby-talk\
**Created:** [20 September 2010 15:21 UTC](https://rubytalk.org/t/convert-time-to-ruby-time-to-calculate-the-time-difference/60134 "2010-09-20T15:21:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ruwan\_Budha](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@Ruwan\_Budha](https://rubytalk.org/u/Ruwan_Budha)\
**Post date:** [20 September 2010 15:21 UTC](https://rubytalk.org/t/convert-time-to-ruby-time-to-calculate-the-time-difference/60134/1 "2010-09-20T15:21:18Z")

</div>

Hi all,

I have following two time stamps,

12:35:10:757 & 12:35:10:759

I want to convert them to ruby time and find the time difference.

which is a 2 micro seconds.

I would appreciate any help..

thanks in advance

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

---

<div class="post-metadata">

**Author:** ![Francesco\_Vollero](https://avatars.discourse-cdn.com/v4/letter/f/c0e974/32.png) [@Francesco\_Vollero](https://rubytalk.org/u/Francesco_Vollero)\
**Post date:** [20 September 2010 15:38 UTC](https://rubytalk.org/t/convert-time-to-ruby-time-to-calculate-the-time-difference/60134/2 "2010-09-20T15:38:42Z")

</div>

> Hi all,
> 
> I have following two time stamps,
> 
> 12:35:10:757& 12:35:10:759
> 
> I want to convert them to ruby time and find the time difference.
> 
> which is a 2 micro seconds.  
> &nbsp;&nbsp;&nbsp;

Simply:

require 'time'

mT=Time.utc(2010,9,20,12,35,10,757)  
=\> Mon Sep 20 12:35:10 UTC 2010  
mT1=Time.utc(2010,9,20,12,35,10,759)  
=\> Mon Sep 20 12:35:10 UTC 2010  
mT1-mT  
=\> 2.0e-06

> I would appreciate any help..
> 
> thanks in advance  
> &nbsp;&nbsp;&nbsp;

You're welcome, but before ask, try to google a bit...[1]

[1] [class Time - RDoc Documentation](http://ruby-doc.org/core/classes/Time.html#M000252)

> **···**
>
> Il 20/09/10 17.21, Ruwan Budha ha scritto:

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [20 September 2010 17:22 UTC](https://rubytalk.org/t/convert-time-to-ruby-time-to-calculate-the-time-difference/60134/3 "2010-09-20T17:22:17Z")

</div>

Alternative:

> > require 'time'

=\> true

> > t1 = Time.parse("12:35:10.000757")

=\> Mon Sep 20 12:35:10 +0100 2010

> > t2 = Time.parse("12:35:10.000759")

=\> Mon Sep 20 12:35:10 +0100 2010

> > t2 - t1

=\> 2.0e-06

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

---

<div class="post-metadata">

**Author:** ![ABHILASH\_M.A](https://avatars.discourse-cdn.com/v4/letter/a/e9c0ed/32.png) [@ABHILASH\_M.A](https://rubytalk.org/u/ABHILASH_M.A)\
**Post date:** [9 March 2011 16:37 UTC](https://rubytalk.org/t/convert-time-to-ruby-time-to-calculate-the-time-difference/60134/4 "2011-03-09T16:37:17Z")

</div>

Brian Candler wrote in post #942410:

> Alternative:
> 
> > > require 'time'
> 
> =\> true
> 
> > > t1 = Time.parse("12:35:10.000757")
> 
> =\> Mon Sep 20 12:35:10 +0100 2010
> 
> > > t2 = Time.parse("12:35:10.000759")
> 
> =\> Mon Sep 20 12:35:10 +0100 2010
> 
> > > t2 - t1
> 
> =\> 2.0e-06

If you got the ruby time, you can use the gem 'time\_diff' to get the  
time difference in a useful format like year, month, week, day, hour,  
minute, second

[https://rubygems.org/gems/time\_diff](https://rubygems.org/gems/time_diff)

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

---

<div class="post-metadata">

**Author:** ![Kirk\_Haines2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/kirk_haines2/32/1992_2.png) [@Kirk\_Haines2](https://rubytalk.org/u/Kirk_Haines2)\
**Post date:** [9 March 2011 16:43 UTC](https://rubytalk.org/t/convert-time-to-ruby-time-to-calculate-the-time-difference/60134/5 "2011-03-09T16:43:14Z")

</div>

I haven't looked at the gem, but....it's all just simple math, except  
for the month part, which takes a bit more work. Does the gem  
\_actually\_ calculate the difference in months between two dates, or  
does it just approximate it?

Kirk Haines  
Software Engineer  
EngineYard

> **···**
>
> On Wed, Mar 9, 2011 at 9:37 AM, ABHILASH M.A \<abhidsm@gmail.com\> wrote:
> 
> > Brian Candler wrote in post #942410:
> > 
> > > Alternative:
> > > 
> > > > > require 'time'
> > > 
> > > =\> true
> > > 
> > > > > t1 = Time.parse("12:35:10.000757")
> > > 
> > > =\> Mon Sep 20 12:35:10 +0100 2010
> > > 
> > > > > t2 = Time.parse("12:35:10.000759")
> > > 
> > > =\> Mon Sep 20 12:35:10 +0100 2010
> > > 
> > > > > t2 - t1
> > > 
> > > =\> 2.0e-06
> > 
> > If you got the ruby time, you can use the gem 'time\_diff' to get the  
> > time difference in a useful format like year, month, week, day, hour,  
> > minute, second
