从Ruby中的DateTime减去n小时 [英] Subtract n hours from a DateTime in Ruby

查看:121
本文介绍了从Ruby中的DateTime减去n小时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Ruby日期时间,从表单中填入。此外,我还有n小时的形式。我想从之前的DateTime中减去n小时。 (获取时间段)。



DateTime有两个方法 - 和<减去日,月,而不是小时。 ( API )。任何建议我可以这样做?

解决方案

你可以这样做。

  adjusted_datetime =(datetime_from_form.to_time  -  n.hours).to_datetime 


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 get a time range).

DateTime has two methods "-" and "<<" to subtract day and month, but not hour. (API). Any suggestions how I can do that?

解决方案

You could do this.

adjusted_datetime = (datetime_from_form.to_time - n.hours).to_datetime

这篇关于从Ruby中的DateTime减去n小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆