给定日期,我如何有效地计算给定序列中的下一个日期(每周,每月,每年)?
在我的应用程序中,我有各种日期序列,例如每周,每月和每年。 鉴于过去的任意日期,我需要计算序列中的下一个未来日期。
目前我正在使用次优循环。 这是一个简化的例子(在Ruby / Rails中):
def calculate_next_date(from_date) next_date = from_date while next_date < Date.today next_date += 1.week # (or 1.month) end next_date end
而不是执行循环(虽然简单,但效率低,特别是在远处过去的日期)我想通过计算两个日期之间的周数(或月,年)来进行日期算术,计算余数并使用这些值生成下一个日期。
这是正确的方法,还是我错过了一种特别聪明的“Ruby”解决方法? 或者我应该坚持我的循环以简化这一切?
因为您将此问题标记为ruby-on-rails
,所以我认为您使用的是Rails。 ActiveSupport引入了计算模块,该模块提供了有用的#advance
方法。
date = Date.today date.advance(:weeks => 1) date.advance(:days => 7) # => next week
为此,我过去曾使用过复发gem 。 还有一些其他gem可以模拟此处列出的重复事件。
如果您使用的是Time
对象,则可以使用Time.to_a
将时间分解为数组(包含表示小时,日,月等的字段),调整相应的字段,并将数组传递回Time.local
或Time.utc
构建一个新的Time
对象。
如果您使用的是Date
类,则date +/- n
将在n天之后/日期为您提供日期,而date >>/<< n
将在n个月之后/之前为您提供日期。
您可以使用更通用的Date.step
而不是循环。 例如,
from_date.step(Date.today, interval) {|d| # Each iteration of this block will be passed a value for 'd' # that is 'interval' days after the previous 'd'. }
其中interval
是以天为单位的时间长度。
如果你所做的只是计算经过的时间,那么可能有更好的方法。 如果您的日期存储为Date
对象,则执行date - Date.today
将为您提供该日期与现在之间的天数。 要计算月,年等,您可以使用以下内容:
# Parameter 'old_date' must be a Date object def months_since(old_date) (Date.today.month + (12 * Date.today.year)) - (old_date.month + (12 * old_date.year)) end def years_since(old_date) Date.today.year - old_date.year end def days_since(old_date) Date.today - old_date end
- 缺少mysql.h …(Ruby on Rails,OSX)
- 元素不是可点击的错误Ruby / Watir
- 链接需要HTTP资产
- 使用Ruby OpenSSL库时无效的公钥
- SSL_connect SYSCALL返回= 5 errno = 0 state = SSLv2 / v3读取服务器hello A – Faraday :: Error :: ConnectionFailed
- Ruby:将美元(String)转换为美分(整数)
- DoubleRenderError(渲染和/或重定向,并在redirect_to或self.render末尾添加’并返回’
- 尝试在Windows,RubyMine IDE中使用Ruby on Rails的LoadError
- 一切都是ruby的对象吗?