Time manipulation in ruby

后端 未结 5 727
北荒
北荒 2021-02-05 02:49

I want to create a DateTime instance that lies 20 minutes and 10 seconds in the future. I tried around with Time and DateTime in irb, but can\'t seem to figure out a way that re

5条回答
  •  [愿得一人]
    2021-02-05 03:35

    As noted above, you can add seconds to a Time object, so if you call to_time on a DateTime object, you can add seconds to it:

    DateTime.strptime("11/19/2019 18:50","%m/%d/%Y %H:%M") + 1 => adds a day
    
    (DateTime.strptime("11/19/2019 18:50","%m/%d/%Y %H:%M").to_time) +1 => adds a second
    

    This doesn't require adding gems.

提交回复
热议问题