Ruby merge time and date to datetime -
i want compose datetime
object time (which constant in model) , date (which attribute stored in database). how can it?
something that:
class product < ar::base manufacture_time = '08:00' # there way better write time? def manufactured_at # method, returns # => 1 jan 2012, 08:00, if self.manufacture_date == date.new(2012, 1, 1) # => 5 oct 2012, 08:00, if self.manufacture_date == date.new(2012, 10, 5) # => 23 sep 2014, 08:00, if self.manufacture_date == date.new(2014, 9, 23) end end
p.s. don't know how store time constant case...
you can construct new datetime using datetime.new
, passing single date components such year , month.
class product < ar::base manufacture_time = '08:00' def manufactured_at hour, minute = manufacture_time.split(':') datetime.new(manufacture_date.year, manufacture_date.month, manufacture_date.day, hour, minute) end end
you can store time want, in array if don't want decompose later using split
.
Comments
Post a Comment