获取每个月的最后一个星期五的那一天

时间:2022-08-08 09:17:08

I am new with ruby and I want to get the day of the last Friday of each month. For example, the last Friday of March is 29, the Last Friday of April 26. So, how can I get a solution? I'm using the rails framework.

我是红宝石的新手,我希望得到每月最后一个星期五的那一天。例如,3月的最后一个星期五是29,即4月26日的最后一个星期五。那么,我该如何获得解决方案呢?我正在使用rails框架。

The method .cweek returns the week of the year, but does not return the week of the current month.

方法.cweek返回一年中的一周,但不返回当月的一周。

4 个解决方案

#1


8  

#!/usr/bin/env ruby

require 'date'

(1..12).each do |month|
  d = Date.new(2013, month, -1)
  d -= (d.wday - 5) % 7
  puts d
end

Source (second/third Google result..)

来源(第二/第三个谷歌结果..)

#2


4  

I would go with Lee's answer, I'm only posting this one because (I thought) it's pretty cool.

我会选择Lee的答案,我只发布这个,因为(我认为)这很酷。

Using gem Chronic (https://github.com/mojombo/chronic):

使用gem Chronic(https://github.com/mojombo/chronic):

#Last Friday of this coming December
require 'chronic'
last_friday = Chronic.parse("First Friday of next January") - 1.week

#3


2  

Retrieving the last friday of a month can be made in a single line:

检索一个月的最后一个星期五可以在一行中进行:

def last_fridays_for_every_month_of_year(year)
  (1..12).map do |month|
    Date.new(year, month, -1).downto(0).find(&:friday?)
  end
end

You may use it like this:

您可以像这样使用它:

last_fridays_for_every_month_of_year 2013
#=> [#<Date: 2013-01-25 ((2456318j,0s,0n),+0s,2299161j)>,
     #<Date: 2013-02-22 ((2456346j,0s,0n),+0s,2299161j)>,
     #<Date: 2013-03-29 ((2456381j,0s,0n),+0s,2299161j)>,
     #<Date: 2013-04-26 ((2456409j,0s,0n),+0s,2299161j)>,
     #<Date: 2013-05-31 ((2456444j,0s,0n),+0s,2299161j)>,
     #<Date: 2013-06-28 ((2456472j,0s,0n),+0s,2299161j)>,
     #<Date: 2013-07-26 ((2456500j,0s,0n),+0s,2299161j)>,
     #<Date: 2013-08-30 ((2456535j,0s,0n),+0s,2299161j)>,
     #<Date: 2013-09-27 ((2456563j,0s,0n),+0s,2299161j)>,
     #<Date: 2013-10-25 ((2456591j,0s,0n),+0s,2299161j)>,
     #<Date: 2013-11-29 ((2456626j,0s,0n),+0s,2299161j)>,
     #<Date: 2013-12-27 ((2456654j,0s,0n),+0s,2299161j)>]

#4


1  

require "active_support/core_ext"

end_of_month = Date.today.end_of_month
if end_of_month - end_of_month.beginning_of_week >= 4
   end_of_month+5.days
else
   end_of_month-2.days
end
# => Fri, 29 Mar 2013

#1


8  

#!/usr/bin/env ruby

require 'date'

(1..12).each do |month|
  d = Date.new(2013, month, -1)
  d -= (d.wday - 5) % 7
  puts d
end

Source (second/third Google result..)

来源(第二/第三个谷歌结果..)

#2


4  

I would go with Lee's answer, I'm only posting this one because (I thought) it's pretty cool.

我会选择Lee的答案,我只发布这个,因为(我认为)这很酷。

Using gem Chronic (https://github.com/mojombo/chronic):

使用gem Chronic(https://github.com/mojombo/chronic):

#Last Friday of this coming December
require 'chronic'
last_friday = Chronic.parse("First Friday of next January") - 1.week

#3


2  

Retrieving the last friday of a month can be made in a single line:

检索一个月的最后一个星期五可以在一行中进行:

def last_fridays_for_every_month_of_year(year)
  (1..12).map do |month|
    Date.new(year, month, -1).downto(0).find(&:friday?)
  end
end

You may use it like this:

您可以像这样使用它:

last_fridays_for_every_month_of_year 2013
#=> [#<Date: 2013-01-25 ((2456318j,0s,0n),+0s,2299161j)>,
     #<Date: 2013-02-22 ((2456346j,0s,0n),+0s,2299161j)>,
     #<Date: 2013-03-29 ((2456381j,0s,0n),+0s,2299161j)>,
     #<Date: 2013-04-26 ((2456409j,0s,0n),+0s,2299161j)>,
     #<Date: 2013-05-31 ((2456444j,0s,0n),+0s,2299161j)>,
     #<Date: 2013-06-28 ((2456472j,0s,0n),+0s,2299161j)>,
     #<Date: 2013-07-26 ((2456500j,0s,0n),+0s,2299161j)>,
     #<Date: 2013-08-30 ((2456535j,0s,0n),+0s,2299161j)>,
     #<Date: 2013-09-27 ((2456563j,0s,0n),+0s,2299161j)>,
     #<Date: 2013-10-25 ((2456591j,0s,0n),+0s,2299161j)>,
     #<Date: 2013-11-29 ((2456626j,0s,0n),+0s,2299161j)>,
     #<Date: 2013-12-27 ((2456654j,0s,0n),+0s,2299161j)>]

#4


1  

require "active_support/core_ext"

end_of_month = Date.today.end_of_month
if end_of_month - end_of_month.beginning_of_week >= 4
   end_of_month+5.days
else
   end_of_month-2.days
end
# => Fri, 29 Mar 2013