# Seconds between two dates

**URL:** <https://www.ruby-forum.com/t/seconds-between-two-dates/157326>\
**Category:** Ruby\
**Created:** [January 9, 2009, 12:30pm UTC](https://www.ruby-forum.com/t/seconds-between-two-dates/157326 "2009-01-09T12:30:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fredini](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e9bcb4/32.png) [@fredini](https://www.ruby-forum.com/u/fredini)\
**Post date:** [January 9, 2009, 12:30pm UTC](https://www.ruby-forum.com/t/seconds-between-two-dates/157326/1 "2009-01-09T12:30:49Z")

</div>

How could I get the seconds between two dates ion Ruby?

I need the seconds remaining from the current date and a date in the  
future.

date1: current date, for example 2009-01-09 17:00:00  
date 2 : 2009-05-08 17:00:00

How could I get the seconds between these two dates?.

Thanks.

---

<div class="post-metadata">

**Author:** ![fredini](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e9bcb4/32.png) [@fredini](https://www.ruby-forum.com/u/fredini)\
**Post date:** [January 9, 2009, 12:40pm UTC](https://www.ruby-forum.com/t/seconds-between-two-dates/157326/2 "2009-01-09T12:40:11Z")

</div>

I think that the following works:

(Time.now-Time.gm(2009,“may”,8,17,00,00)).to\_i

blambeau

---

<div class="post-metadata">

**Author:** ![fredini](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e9bcb4/32.png) [@fredini](https://www.ruby-forum.com/u/fredini)\
**Post date:** [January 9, 2009, 12:55pm UTC](https://www.ruby-forum.com/t/seconds-between-two-dates/157326/3 "2009-01-09T12:55:26Z")

</div>

On 9 ene, 12:39, LAMBEAU Bernard [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > future.
> 
> > date1: current date, for example 2009-01-09 17:00:00  
> > date 2 : 2009-05-08 17:00:00
> 
> > How could I get the seconds between these two dates?.
> 
> > Thanks.

I think it’s working but the operands need to be changed to get the  
positive difference in seconds that I needed.

(Time.gm(2009,“may”,8,17,00,00)-Time.now).to\_i

thank you very much blambeau.
