There is a date

t1 = "Sat, 30 Dec 1899 00:00:00 +0000"; // DateTime 

And there is a string like HH: MM

 t2 = "07:05"; 

How to add this clock to t1? To succeed

 Sat, 30 Dec 1899 07:05:00 +0000" 

    2 answers 2

     require "time" Time.parse(t1) + (Time.parse(t2) - Date.today.to_time) 

    1899-12-30 10:05:00 +0300

      I already told you about the durations earlier , and so they can be added not only to each other, but also to the moments presented in the Time type. Get new moments. This is a good example of how the PLO imitates the concepts of our world and operations on them.

       require "time" base = Time.parse("Sat, 30 Dec 1899 00:00:00 +0000") hh, mm = "07:05".split(":").map(&:to_i) 

      The result can be obtained:

      ... adding to the starting point the number of seconds of the duration you need ( see Time#+ ):

       base + (hh * 3600) + (mm * 60) # интуитивно это самый шустрый способ, но я не тестировал 

      ... or using ActiveSupport::Duration :

       base + (hh.hours + mm.minutes) # скобки не особо нужны, на самом деле