gpt4 book ai didi

ruby-on-rails - 获取从周五开始的周列表

转载 作者:数据小太阳 更新时间:2023-10-29 08:03:21 26 4
gpt4 key购买 nike

我想统计周数,从星期五开始。

更多解释:通常计数从周日或周一开始,但在这种情况下,我想在周五开始。

一些例子:

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

注意:您可以设置任何范围。这里我设置了从今天明年

%U - Week number of the current year, starting with the first Sunday as the first day of the first week (00..53)

%W - Week number of the current year, starting with the first Monday as the firstday of the first week (00..53)

了解更多信息:http://apidock.com/rails/ActiveSupport/TimeWithZone/strftime

关于ruby-on-rails - 获取从周五开始的周列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33431120/

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