我想从周五开始数周数。
更多解释:通常计数从周日或周一开始,但在这种情况下,我想让它从周五开始。
一些例子:
2nd Jan 2015 (Fri) ~ 8th Jan 2015 (Thu) : 1st week of 2015
...
25th Dec 2015 (Fri) ~ 31st Dec 2015 (Thu) : 52nd week of 2015
1st Jan 2016 (Fri) ~ 7th Jan 2016 (Thu) : 1st week of 2016
...
30th Dec 2016 (Fri) ~ 5th Jan 2017 (Thu) : 53rd week of 2016
6th Jan 2017 (Fri) ~ 12th Jan 2017 (Thu) : 1st week of 2017
我需要做的是
1)从日期中获取周数
前任。
input: Fri, 02 Jan 2015
output: 1
input: Sun, 27 Dec 2015
output: 52
2) 获取给定周数的日期范围。
我发现
.strftime("%V")
几乎可以做到这一点,但它每周一计算数周。有谁知道解决这个问题的好方法?
最佳答案
干得好:
my_array = Array.new
(Date.today..Date.today.next_year).each do |date|
if date.wday == 5
end_week = date + 6.days
my_array << "#{date.day.ordinalize} #{date.strftime('%b')} #{date.strftime('%Y')} (#{date.strftime('%a')}) ~ #{end_week.day.ordinalize} #{end_week.strftime('%b')} #{end_week.strftime('%Y')} (#{end_week.strftime('%a')}) : #{date.strftime('%U')} week of #{date.strftime('%Y')}"
end
end
# sample output of first element of array
# 30th Oct 2015 (Fri) ~ 5th Nov 2015 (Thu) : 43 week of 2015
注意: 可以设置任意范围。这里我从
Today
设置为 next year
更多信息:http://apidock.com/rails/ActiveSupport/TimeWithZone/strftime
关于ruby-on-rails - 获取从周五开始的周列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33431120/