Ruby-on-rails 如何将分钟添加到 Time 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6936203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:42:51  来源:igfitidea点击:

How to add minutes to a Time object

ruby-on-railsrubytime

提问by keruilin

In Ruby, how do I do Time.now + 10.hours?

在 Ruby 中,我该怎么做Time.now + 10.hours

Is there an equivalent for secsand mins? For example:

是否有一个等效secsmins?例如:

Time.now + 15.mins

回答by Phrogz

Ruby (the programming language) doesn't have 10.hours, that's ActiveSupportas part of Ruby on Rails (the web framework). And yes, it does have both minutesand secondsmethods.

Ruby(编程语言)没有10.hours,它ActiveSupport是 Ruby on Rails(Web 框架)的一部分。是的,它确实有minutesseconds方法。

However, Time#+(the + method on Time instances) returns a new Time instance that is that many seconds in the future. So without any Ruby on Rails sugar, you can simply do:

但是,Time#+(Time 实例上的 + 方法)返回一个新的 Time 实例,该实例是未来几秒钟的时间。因此,没有任何 Ruby on Rails 糖,您可以简单地执行以下操作:

irb> t = Time.now
#=> 2011-08-03 22:35:01 -0600

irb> t2 = t + 10               # 10 Seconds
#=> 2011-08-03 22:35:11 -0600

irb> t3 = t + 10*60            # 10 minutes
#=> 2011-08-03 22:45:01 -0600

irb> t4 = t + 10*60*60         # 10 hours
#=> 2011-08-04 08:35:01 -0600

回答by twe4ked

If you are using ActiveSupport, what you are looking for is the full .minutesand .seconds.

如果您正在使用 ActiveSupport,那么您正在寻找的是完整的.minutes.seconds.

Time.now + 10.minutes
Time.now + 10.seconds

回答by mahi-man

Also in ActiveSupport you can do:

同样在 ActiveSupport 中,您可以执行以下操作:

10.minutes.from_now

10.minutes.ago

回答by Gishu

I think you're talking about extensions added by Rails. I think you need 15.minutes.

我认为您在谈论 Rails 添加的扩展。我认为你需要15.minutes.

See the Active Support Core Extensions for Date, DateTime and Timefor more information.

有关详细信息请参阅日期、日期时间和时间活动支持核心扩展

回答by Sushant

Time Object

时间对象

time = Time.now

time = Time.now

Adding minutes to a time object:

向时间对象添加分钟:

time + 5.minutes

time + 5.minutes