Subtract n hours from a DateTime in Ruby

后端 未结 11 1343
别那么骄傲
别那么骄傲 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 07:05

    You didn't say what use you need to make of the value you get, but what about just dividing your hours by 24 so you're subtracting a fraction of a day?

    mydatetime = DateTime.parse(formvalue)
    nhoursbefore = mydatetime - n / 24.0
    

提交回复
热议问题