new Date from Time.now().to_a

A

Aaron Smith

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?
 
R

Ryan Davis

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 =3D Time.now
ms =3D ("%.6f" % tn.to_f) #this would ultimately be comming from = another
source
nd =3D Time.at( ms.to_f / 1000.0 )
puts nd.to_a.inspect

Remember, ri is your friend=99.

% ri Date::civil
------------------------------------------------------------ Date::civil
Date::civil(y=3D-4712, m=3D1, d=3D1, sg=3DITALY)
------------------------------------------------------------------------
 
A

Aaron Smith

Ryan said:
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
 
A

Aaron Smith

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]}")
 
R

Robert Klemme

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]}")

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,237
Messages
2,571,189
Members
47,825
Latest member
XCCMilo924

Latest Threads

Top