# Ruby datetime

**URL:** <https://rubytalk.org/t/ruby-datetime/54059>\
**Category:** ruby-talk\
**Created:** [23 June 2009 11:33 UTC](https://rubytalk.org/t/ruby-datetime/54059 "2009-06-23T11:33:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sriram\_Varahan](https://avatars.discourse-cdn.com/v4/letter/s/c68b51/32.png) [@Sriram\_Varahan](https://rubytalk.org/u/Sriram_Varahan)\
**Post date:** [23 June 2009 11:33 UTC](https://rubytalk.org/t/ruby-datetime/54059/1 "2009-06-23T11:33:24Z")

</div>

Hello,

I have two time objects:

# 2 days ago  
time1 = Time.now - 48 \* 3600

#=\> Sun Jun 21 16:56:33 +0530 2009

time2 = Time.now  
#=\> Tue Jun 23 16:57:05 +0530 2009

I need to create a third time object which will have the date from time2  
and the time from time1.

time3 should be:

#=\> Tue Jun 23 16:56:33 +0530 2009

The issue I am facing is how do I extract the date and time to create  
the third time.

Any suggestion would be welcome.

Thanks  
Sriram.

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

---

<div class="post-metadata">

**Author:** ![Shot\_Piotr\_Szotkowsk](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@Shot\_Piotr\_Szotkowsk](https://rubytalk.org/u/Shot_Piotr_Szotkowsk)\
**Post date:** [23 June 2009 11:56 UTC](https://rubytalk.org/t/ruby-datetime/54059/2 "2009-06-23T11:56:31Z")

</div>

Sriram Varahan:

> I have two time objects:

> # 2 days ago  
> time1 = Time.now - 48 \* 3600

> #=\> Sun Jun 21 16:56:33 +0530 2009

> time2 = Time.now  
> #=\> Tue Jun 23 16:57:05 +0530 2009

> I need to create a third time object which will have the date from time2  
> and the time from time1.

time3 = time1 + 48 \* 3600

(Although I think if you let us know \*why\* you need  
these dates, a simpler solution would be possible.)

— Shot

> **···**
>
> --  
> In Focus: At Home, Donald Knuth Mows His N × N Lawn With Stunning O(N log N)  
> Algorithm That He ‘Whipped Up One Morning Over Coffee’ [patentlyfalse]

---

<div class="post-metadata">

**Author:** ![Rick\_DeNatale1](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rick\_DeNatale1](https://rubytalk.org/u/Rick_DeNatale1)\
**Post date:** [23 June 2009 11:58 UTC](https://rubytalk.org/t/ruby-datetime/54059/3 "2009-06-23T11:58:48Z")

</div>

time3 = Time.local(time2.year, time2.month, time2.mday, time1.hour,  
time1.min, time1. sec, time1.usec)

> **···**
>
> On Tue, Jun 23, 2009 at 7:33 AM, Sriram Varahan\<sriram.varahan@gmail.com\> wrote:
> 
> > Hello,
> > 
> > I have two time objects:
> > 
> > # 2 days ago  
> > time1 = Time.now - 48 \* 3600
> > 
> > #=\> Sun Jun 21 16:56:33 +0530 2009
> > 
> > time2 = Time.now  
> > #=\> Tue Jun 23 16:57:05 +0530 2009
> > 
> > I need to create a third time object which will have the date from time2  
> > and the time from time1.
> > 
> > time3 should be:
> > 
> > #=\> Tue Jun 23 16:56:33 +0530 2009
> > 
> > The issue I am facing is how do I extract the date and time to create  
> > the third time.
> > 
> > Any suggestion would be welcome.
> 
> --  
> Rick DeNatale
> 
> Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
> Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
> WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
> LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![Heesob\_Park](https://avatars.discourse-cdn.com/v4/letter/h/3bc359/32.png) [@Heesob\_Park](https://rubytalk.org/u/Heesob_Park)\
**Post date:** [23 June 2009 13:30 UTC](https://rubytalk.org/t/ruby-datetime/54059/4 "2009-06-23T13:30:24Z")

</div>

Hi,

> **···**
>
> 2009/6/23 Sriram Varahan \<sriram.varahan@gmail.com\>:
> 
> > Hello,
> > 
> > I have two time objects:
> > 
> > # 2 days ago  
> > time1 = Time.now - 48 \* 3600
> > 
> > #=\> Sun Jun 21 16:56:33 +0530 2009
> > 
> > time2 = Time.now  
> > #=\> Tue Jun 23 16:57:05 +0530 2009
> > 
> > I need to create a third time object which will have the date from time2  
> > and the time from time1.
> > 
> > time3 should be:
> > 
> > #=\> Tue Jun 23 16:56:33 +0530 2009
> > 
> > The issue I am facing is how do I extract the date and time to create  
> > the third time.
> > 
> > Any suggestion would be welcome.
> 
> time3 = Time.at(time2.to\_i/86400\*86400+time1.to\_f%86400)
> 
> Regards,  
> Park Heesob
