用于获取下周一(或一周中的任何一天)的日期的 Ruby 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7930370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 04:25:44  来源:igfitidea点击:

Ruby code to get the date of next Monday (or any day of the week)

rubydate

提问by Panagiotis Panagi

Given an input of, for example,

给定输入,例如,

day = 'Monday'

how can I calculate the date of day?

如何计算日期day

def date_of_next(day)
  ...
end

回答by fl00r

require 'date'

def date_of_next(day)
  date  = Date.parse(day)
  delta = date > Date.today ? 0 : 7
  date + delta
end

Date.today
#=>#<Date: 2011-10-28 (4911725/2,0,2299161)>
date_of_next "Monday"
#=>#<Date: 2011-10-31 (4911731/2,0,2299161)>
date_of_next "Sunday"
#=>#<Date: 2011-10-30 (4911729/2,0,2299161)>

回答by Devin B

I know this is an old post, but I came up with a couple of methods to quickly get the previous and next day of the week.

我知道这是一篇旧帖子,但我想出了几种方法来快速获取一周的前一天和后一天。

# date is a Date object and day_of_week is 0 to 6 for Sunday to Saturday

require 'Date'

def get_next_day(date, day_of_week)
  date + ((day_of_week - date.wday) % 7)
end

def get_previous_day(date, day_of_week)
  date - ((date.wday - day_of_week) % 7)
end

puts today = Date.today
# 2015-02-24

puts next_friday = get_next_day(today, 5)
# 2015-02-27

puts last_friday = get_previous_day(today, 5)
# 2015-02-20

回答by s_dolan

For anyone like me who came here looking for a solution in Rails to this problem, as of Rails 5.2 there is a much easier method to do this.

对于像我这样来这里寻找 Rails 解决方案的人来说,从 Rails 5.2 开始,有一种更简单的方法可以做到这一点。

For anyone (like the original poster) not specifically using Rails, this functionality is available in the ActiveSupportgem.

对于没有专门使用 Rails 的任何人(如原始海报),ActiveSupportgem 中提供了此功能。

To find the next occurring day of a week, we can simply write something like Date.today.next_occurring(:friday).

要找到一周中的下一个发生日,我们可以简单地编写类似Date.today.next_occurring(:friday).

See the documentationfor more details.

有关更多详细信息,请参阅文档

回答by Dave Newton

For stuff like this I rely on the chroniclibrary.

对于这样的东西,我依赖慢性图书馆。

The Ruby code would be:

Ruby 代码将是:

def date_of_next(day)
    Chronic.parse("next #{day}")
end

回答by Hannu

If you are using rails you can use Date.today.sundayfor Sunday or Date.today.mondayfor Monday. And then Date.today.sunday - 1.dayfor Saturday etc.

如果您使用的是导轨,则可以Date.today.sunday在周日或Date.today.monday周一使用。然后Date.today.sunday - 1.day星期六等。

回答by Milo P

A Rails 4-compatible solution:

Rails 4 兼容的解决方案:

(Date.today + 1.week).beginning_of_week(:monday)

(Date.today + 1.week).beginning_of_week(:monday)

where you can provide the day you'd like to find as a symbol argument; the default is :monday.

您可以在其中提供您想要查找的日期作为符号参数;默认为:monday.

Note that this will find the nextoccurrence of the given day - if today is Monday and you're looking for the next Monday, it will return the Monday one week from today.

请注意,这将查找给定日期的下一次出现 - 如果今天是星期一并且您正在寻找下一个星期一,它将从今天起一周返回星期一。

Some equivalent ways to do it:

一些等效的方法来做到这一点:

1.week.from_now.beginning_of_week(:monday).to_date

1.week.from_now.beginning_of_week(:monday).to_date

1.week.since(Date.today).beginning_of_week(:monday)

1.week.since(Date.today).beginning_of_week(:monday)

(source: https://apidock.com/rails/Date/beginning_of_week/class)

(来源:https: //apidock.com/rails/Date/beginning_of_week/class