Forum: Ruby new Date from Time.now().to_a

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5e8fd591aeccbffd6fc93c171ac4fa43?d=identicon&s=25 Aaron Smith (warhero)
on 2007-03-12 02:02
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?
5a837592409354297424994e8d62f722?d=identicon&s=25 Ryan Davis (Guest)
on 2007-03-12 02:06
(Received via mailing list)
On Mar 11, 2007, at 6:02 PM, Aaron Smith 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)
5e8fd591aeccbffd6fc93c171ac4fa43?d=identicon&s=25 Aaron Smith (warhero)
on 2007-03-12 02:24
Ryan Davis wrote:
> On Mar 11, 2007, at 6:02 PM, Aaron Smith 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
5e8fd591aeccbffd6fc93c171ac4fa43?d=identicon&s=25 Aaron Smith (warhero)
on 2007-03-12 02:27
> 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]}")
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-03-12 20:05
(Received via mailing list)
On 12.03.2007 02:27, Aaron Smith 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
5e8fd591aeccbffd6fc93c171ac4fa43?d=identicon&s=25 Aaron Smith (warhero)
on 2007-03-12 20:50
Robert Klemme wrote:
> On 12.03.2007 02:27, Aaron Smith 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.
This topic is locked and can not be replied to.