将ActiveRecord日期和时间组合到DateTime中

at.*_*at. 3 activerecord ruby-on-rails ruby-on-rails-3.2 ruby-on-rails-4 rails-activerecord

我的数据库中的表中有2列,1是日期,1是时间.我需要使用ActiveRecord将它们组合成DateTime对象.什么是最好的方式?

1.9.3-p194 :053 > date.class
 => Date 
1.9.3-p194 :054 > time.class
 => Time 
1.9.3-p194 :055 > date.to_time_in_current_zone
 => Wed, 04 Sep 2013 00:00:00 PDT -07:00 
1.9.3-p194 :056 > date.to_time_in_current_zone + time
TypeError: not an integer
Run Code Online (Sandbox Code Playgroud)

我可以添加时间的各个字段,这似乎工作:

1.9.3-p194 :062 > date.to_time_in_current_zone + time.hour.hours + time.min.minutes + time.sec.seconds
 => Wed, 04 Sep 2013 12:30:00 PDT -07:00 
Run Code Online (Sandbox Code Playgroud)

那不包括毫秒,没有Time毫秒?如果我不能有毫秒,那么不是世界末日,但是有更好的方法吗?

Sag*_*til 10

很抱歉在中间窥视我实际上是在8个月左右的新铁路,它就像一个小人在大人之间说话.我也面临同样的问题,我通过使用来获得这个

date_time= (date.to_s + " " +time.to_s).to_datetime
Run Code Online (Sandbox Code Playgroud)

我不知道它是对还是错,但我的问题已经解决了.Plz确实给了我关于这个的反馈:)

此外,Time还可以#strftime使用%Lformat指令访问毫秒.此外,可以通过使用来访问较小的秒数部分%N.