2013-10-07 53 views
0

對不起我的英語...總和日期對象,時間對象上的回報率

我有兩個目標,一個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)> 

有些幫助嗎?

謝謝!

回答

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 
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)