Subtract n hours from a DateTime in Ruby

后端 未结 11 1353
别那么骄傲
别那么骄傲 2020-12-14 06:02

I have a Ruby DateTime which gets filled from a form. Additionally I have n hours from the form as well. I\'d like to subtract those n hours from the previous DateTime. (To

11条回答
  •  没有蜡笔的小新
    2020-12-14 06:45

    If I'm allowed to use Time instead of DateTime (There are several ways to translate one to another):

    # Just remove the number of seconds from the Time object
    Time.now - (6 * 60 * 60) # 6 hours ago
    

提交回复
热议问题