我正在尝试在特定时期内进行报告,支持的时期是:
listing-full-width-3column-grid.php
grid-threecolumn.php
start = DateTime.now.beginning_of_day; end = start.end_of_day
当前日期的每月的前半或后半)
start = DateTime.now.beginning_of_week; end = start.end_of_week
;第一个双周的开始应该是该月的第一天,而第二个双周的结束应该是该月的最后一个。两者之间的划分日期应为该月的天总数除以2,然后四舍五入(即,如果有31天,则第二个双周将从该月的15号开始,而不是该月的14号开始)。 DateTime.now
cweek
start = DateTime.now.beginning_of_month; end = start.end_of_month
我无法根据当前日期计算粗体日期范围(假设start = (DateTime.now - 1.month).beginning_of_month; end = start.end_of_month
)。
如何计算相对于当前日期start = DateTime.now.beginning_of_year --> end = start.end_of_year
的每两周和每季度?
对于日期为2018年5月7日(5/7/2018):
DateTime.now
至DateTime.now
5/1/2018
至5/15/2018
对于日期2020年2月29日(2/29/2020),a年:
4/1/2018
至6/30/2018
2/15/2018
至2/29/2018
答案 0 :(得分:2)
那呢:
本双周:
def bi_week_limits_for(date)
days_in_month = Time.days_in_month(date.month, date.year)
# this will round down. 30/31-day months will output 15. 28 / 29-day will output 14. Adjust as per your requirements
middle_day = days_in_month / 2
if date.day <= middle_day
[date.beginning_of_month, date.change(day: middle_day)]
else
[date.change(day: middle_day + 1), date.end_of_month]
end
end
在我的控制台中:
pry(main)> bi_week_limits_for Date.parse('29/2/2020')
=> [Sat, 15 Feb 2020, Sat, 29 Feb 2020]
pry(main)> bi_week_limits_for Date.parse('7/5/2018')
=> [Tue, 01 May 2018, Tue, 15 May 2018]
本季度:
def bi_week_limits_for(date)
[date.beginning_of_quarter, date.end_of_quarter]
end
在我的控制台中
pry(main)> date = Date.parse('7/5/2018')
=> Mon, 07 May 2018
pry(main)> quarter_limits_for date
=> [Sun, 01 Apr 2018, Sat, 30 Jun 2018]
pry(main)> date = Date.parse '29/2/2020'
=> Sat, 29 Feb 2020
pry(main)> quarter_limits_for date
=> [Wed, 01 Jan 2020, Tue, 31 Mar 2020]
参考:https://apidock.com/rails/DateAndTime/Calculations/beginning_of_quarter