Search code examples
ruby-on-railsrubydatetimetimeactivesupport

Equivalent of Active Support Time change method in core ruby


With ActiveSupport Time extension I can say DateTime.new(2016,3,16).change(hour: 14) and change gets 2 o'clock in the afternoon of that day => Wed, 16 Mar 2016 14:00:00 +0000.

Is there a similar way in plain ruby? which is also this neat and simple?


Solution

  • This seems simple enough:
    
    irb(main):004:0> dt = DateTime.new(2016,3,16)
    => #<DateTime: 2016-03-16T00:00:00+00:00 ((2457464j,0s,0n),+0s,2299161j)>
    irb(main):005:0> dt = DateTime.new(dt.year, dt.month, dt.day, 14, 0, 0, 0)
    => #<DateTime: 2016-03-16T14:00:00+00:00 ((2457464j,50400s,0n),+0s,2299161j)>
    irb(main):006:0>