gpt4 book ai didi

ruby-on-rails - Rails 4 : How to add/subtract multiples of quarters to Date?

转载 作者:行者123 更新时间:2023-12-04 05:51:12 26 4
gpt4 key购买 nike

要获取当前季度的开始日期和结束日期,可以执行以下操作:

start_of_curr_quarter = Time.current.to_date.beginning_of_quarter
end_of_curr_quarter = start_of_curr_quarter.end_of_quarter

要获得上一季度的日期,这似乎是一种合理的方法:
end_of_prev_quarter = Time.current.to_date.beginning_of_quarter - 1
start_of_prev_quarter = end_of_prev_quarter.beginning_of_quarter

我想对此进行概括。您将如何在Rails 4中实现以下功能?
def quarter_dates(quarter_offset)
# --------------------------------------------
# quarter_offset return value
# --------------------------------------------
# 0 current quarter dates
# 1 prev quarter dates
# 2 before prev quarter dates
# ... ...

# Implementation goes here...

[start_of_quarter, end_of_quarter]
end

最佳答案

我会这样:

def quarter_dates(offset)
date = Date.today << (offset * 3)
[date.beginning_of_quarter, date.end_of_quarter]
end

puts quarter_dates(0) #=> [Tue, 01 Apr 2014, Mon, 30 Jun 2014]
puts quarter_dates(1) #=> [Wed, 01 Jan 2014, Mon, 31 Mar 2014]
puts quarter_dates(9) #=> [Sun, 01 Jan 2012, Sat, 31 Mar 2012]

关于ruby-on-rails - Rails 4 : How to add/subtract multiples of quarters to Date?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24182829/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com