# New Date from Time.now().to\_a

**URL:** <https://www.ruby-forum.com/t/new-date-from-time-now-to-a/88492>\
**Category:** Ruby\
**Created:** [March 12, 2007, 2:02am UTC](https://www.ruby-forum.com/t/new-date-from-time-now-to-a/88492 "2007-03-12T02:02:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [March 12, 2007, 2:02am UTC](https://www.ruby-forum.com/t/new-date-from-time-now-to-a/88492/1 "2007-03-12T02:02:37Z")

</div>

I don’t see any obvious ways of making a new Date object based off of  
what Time.now rerurns. I’m ultimately trying to make a new Date from  
milliseconds:

tn = Time.now  
ms = ("%.6f" % tn.to\_f) #this would ultimately be comming from another  
source  
nd = Time.at( ms.to\_f / 1000.0 )  
puts nd.to\_a.inspect  
#d = Date.new(??)

The next step is making the date object. I don’t see anything obvious in  
the Date class. Any help?

---

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [March 12, 2007, 2:06am UTC](https://www.ruby-forum.com/t/new-date-from-time-now-to-a/88492/2 "2007-03-12T02:06:21Z")

</div>

On Mar 11, 2007, at 6:02 PM, Aaron S. wrote:

> I don’t see any obvious ways of making a new Date object based off of  
> what Time.now rerurns. I’m ultimately trying to make a new Date from  
> milliseconds:
> 
> tn = Time.now  
> ms = ("%.6f" % tn.to\_f) #this would ultimately be comming from another  
> source  
> nd = Time.at( ms.to\_f / 1000.0 )  
> puts nd.to\_a.inspect

Remember, ri is your friend™.

% ri Date::civil  
------------------------------------------------------------ Date::civil  
Date::civil(y=-4712, m=1, d=1, sg=ITALY)

---

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [March 12, 2007, 2:24am UTC](https://www.ruby-forum.com/t/new-date-from-time-now-to-a/88492/3 "2007-03-12T02:24:46Z")

</div>

Ryan D. wrote:

> On Mar 11, 2007, at 6:02 PM, Aaron S. wrote:
> 
> > I don’t see any obvious ways of making a new Date object based off of  
> > what Time.now rerurns. I’m ultimately trying to make a new Date from  
> > milliseconds:
> > 
> > tn = Time.now  
> > ms = ("%.6f" % tn.to\_f) #this would ultimately be comming from another  
> > source  
> > nd = Time.at( ms.to\_f / 1000.0 )  
> > puts nd.to\_a.inspect
> 
> Remember, ri is your friendï¿½.
> 
> % ri Date::civil  
> ------------------------------------------------------------ Date::civil  
> Date::civil(y=-4712, m=1, d=1, sg=ITALY)

require ‘date’

ms = 1173658884500.0  
nd = Time.at( ms.to\_f / 1000.0 )  
#puts nd  
#puts nd.to\_a.inspect  
f = nd.to\_a  
n = Date.parse("#{f[4]}-#{f[3]}-#{f[5]} #{f[1]}:#{f[2]}:#{f[3]}  
#{f[9]}")  
#puts n  
#puts n.class

---

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [March 12, 2007, 2:27am UTC](https://www.ruby-forum.com/t/new-date-from-time-now-to-a/88492/4 "2007-03-12T02:27:56Z")

</div>

> ms = 1173658884500.0  
> nd = Time.at( ms.to\_f / 1000.0 )  
> #puts nd  
> #puts nd.to\_a.inspect  
> f = nd.to\_a  
> n = Date.parse("#{f[4]}-#{f[3]}-#{f[5]} #{f[1]}:#{f[2]}:#{f[3]}  
> #{f[9]}")  
> #puts n  
> #puts n.class

correction: Date.parse("#{f[3]}-#{f[4]}-#{f[5]} #{f[1]}:#{f[2]}:#{f[3]}  
#{f[9]}")

---

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [March 12, 2007, 8:05pm UTC](https://www.ruby-forum.com/t/new-date-from-time-now-to-a/88492/5 "2007-03-12T20:05:29Z")

</div>

On 12.03.2007 02:27, Aaron S. wrote:

> correction: Date.parse("#{f[3]}-#{f[4]}-#{f[5]} #{f[1]}:#{f[2]}:#{f[3]}  
> #{f[9]}")

Why the parsing?

irb(main):042:0\> t=Time.now  
=\> Mon Mar 12 19:59:33 +0100 2007  
irb(main):043:0\> d=Date.new(t.year, t.month, t.day)  
=\> #\<Date: 4908343/2,0,2299161\>  
irb(main):044:0\> d.strftime  
=\> “2007-03-12”

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [March 12, 2007, 8:50pm UTC](https://www.ruby-forum.com/t/new-date-from-time-now-to-a/88492/6 "2007-03-12T20:50:07Z")

</div>

Robert K. wrote:

> On 12.03.2007 02:27, Aaron S. wrote:
> 
> > correction: Date.parse("#{f[3]}-#{f[4]}-#{f[5]} #{f[1]}:#{f[2]}:#{f[3]}  
> > #{f[9]}")
> 
> Why the parsing?

robert

thanks. thats exactly what I was looking for.
