Ruby: combine Date and Time objects into a DateTime

后端 未结 6 1633
伪装坚强ぢ
伪装坚强ぢ 2020-12-08 13:18

Simple question, but I can\'t find a good or definitive answer. What is the best and most efficient way to combine Ruby Date and Time objects (objects, not

6条回答
  •  醉梦人生
    2020-12-08 14:01

    I found this, but it's not as elegant you would hope:

    d = Date.new(2012, 8, 29)
    t = Time.now
    dt = DateTime.new(d.year, d.month, d.day, t.hour, t.min, t.sec, t.zone)
    

    By the way, the ruby Time object also stores a year, month, and day, so you would be throwing that away when you create the DateTime.

提交回复
热议问题