我有两个对象,一个Date对象和一个Time对象,我想要求它们得到一个DateTime对象,这是我在尝试...
[35] pry(main)> dateobj = Date.today
=> Sun, 06 Oct 2013
[36] pry(main)> timeobj = Time.parse("02:00:00")
=> 2013-10-06 02:00:00 -0600
[37] pry(main)> datetimeobj = dateobj + timeobj
TypeError: expected numeric
from /home/elquick/www/rails/vivsan/http/vendor/bundle/ruby/2.0.0/gems/activesupport-3.2.14/lib/active_support/core_ext/date/calculations.rb:90:in `+'
[38] pry(main)>
一些帮助?
谢谢!
答案 0 :(得分:2)
尝试使用
Date.today.to_datetime + Time.parse('02:00:00').seconds_since_midnight.seconds
这是我的结果:
.0.0-p195 :019 > Date.today.to_datetime
=> Mon, 07 Oct 2013 00:00:00 +0000
2.0.0-p195 :020 > Time.parse('02:00:00').seconds_since_midnight.seconds
=> 7200.0 seconds
2.0.0-p195 :021 > Date.today.to_datetime + Time.parse('02:00:00').seconds_since_midnight.seconds
=> Mon, 07 Oct 2013 02:00:00 +0000
答案 1 :(得分:0)
我发现了这个......
dateobj = Date.new(2013, 10, 6)
timeobj = Time.parse("02:00:00")
datetime = DateTime.new(dateobj.year, dateobj.month, dateobj.day, timeobj.hour, timeobj.min, timeobj.sec)