找到下周的第一天 - Ruby on Rails

时间:2022-08-01 21:30:10

I need to define which comes first before I run the def. I have to rake a file and set the date for the coming week BUT that date could be either Monday or Wednesday. I've gotten it thus far but not sure how to judge which date to use in this. I need to use whatever date comes first.

在运行def之前,我需要先定义哪个是第一个。我必须耙一个文件并设置下周的日期,但该日期可能是星期一或星期三。到目前为止,我已经得到了它,但不知道如何判断在哪个日期使用。我需要先使用任何日期。

require 'date'

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

hello = date_of_next("Monday", "Wednesday")
puts hello

The Problem: If I am on Saturday, I need to get the date for the coming "Monday". But, if I am on Tuesday, I need to get the date of "Wednesday". When I have the task finalized, it will run daily getting this information.

问题:如果我在星期六,我需要得到即将到来的“星期一”的日期。但是,如果我在星期二,我需要得到“星期三”的日期。当我完成任务时,它将每天运行以获取此信息。

4 个解决方案

#1


1  

You code is based on https://*.com/a/7930553/477037:

您的代码基于https://*.com/a/7930553/477037:

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

The above method returns the date for a single day. To find the first date for multiple days, call it for each day and find the first one (by sorting the dates):

上述方法返回一天的日期。要查找多天的第一个日期,请每天调用它并查找第一个日期(通过对日期进行排序):

def first_date_of_next(*days)
  days.map { |day| date_of_next(day) }.sort.first
end

first_date_of_next("Monday", "Wednesday")
#=> #<Date: 2013-10-16 ((2456582j,0s,0n),+0s,2299161j)>

#2


0  

How about this?

这个怎么样?

require 'Date'

def date_of_next(*day_names)
  weekdays = day_names.map{|dayname| Date::DAYNAMES.index(day_name) }
  Range.new(Date.today, Date.today+6).detect{|day| weekdays.include? day.wday }
end

Here we're globbing the input allowing you to detect one or more weekday names. The first line finds the index of the name of the weekday in the Date::DAYNAMES array. In the second line we process up to a week's worth of dates returning when the first day.wday (weekday index) matches one of the weekdays we're looking for.

在这里,我们将输入全局化,允许您检测一个或多个工作日名称。第一行在Date :: DAYNAMES数组中查找工作日名称的索引。在第二行,我们处理最多一周的日期返回,当第一天.wday(工作日索引)与我们正在寻找的工作日之一匹配时。

#3


0  

require 'date'

def date_of_next(d)
  Date.today.step(Date.today + 7).select{ |i| puts i if i.strftime("%A") == "#{d}" && i > Date.today} 
end

#4


0  

Would this meet your requirements?

这会满足您的要求吗?

require 'Date'
def date_of_next(early_dow, late_dow)
  early_date = Date.parse(early_dow)
  late_date = Date.parse(late_dow)
  today = Date.today
  if today <= early_date
    early_date
  elsif today <= late_date
    late_date
  else
    early_date + 7
  end
end      

#1


1  

You code is based on https://*.com/a/7930553/477037:

您的代码基于https://*.com/a/7930553/477037:

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

The above method returns the date for a single day. To find the first date for multiple days, call it for each day and find the first one (by sorting the dates):

上述方法返回一天的日期。要查找多天的第一个日期,请每天调用它并查找第一个日期(通过对日期进行排序):

def first_date_of_next(*days)
  days.map { |day| date_of_next(day) }.sort.first
end

first_date_of_next("Monday", "Wednesday")
#=> #<Date: 2013-10-16 ((2456582j,0s,0n),+0s,2299161j)>

#2


0  

How about this?

这个怎么样?

require 'Date'

def date_of_next(*day_names)
  weekdays = day_names.map{|dayname| Date::DAYNAMES.index(day_name) }
  Range.new(Date.today, Date.today+6).detect{|day| weekdays.include? day.wday }
end

Here we're globbing the input allowing you to detect one or more weekday names. The first line finds the index of the name of the weekday in the Date::DAYNAMES array. In the second line we process up to a week's worth of dates returning when the first day.wday (weekday index) matches one of the weekdays we're looking for.

在这里,我们将输入全局化,允许您检测一个或多个工作日名称。第一行在Date :: DAYNAMES数组中查找工作日名称的索引。在第二行,我们处理最多一周的日期返回,当第一天.wday(工作日索引)与我们正在寻找的工作日之一匹配时。

#3


0  

require 'date'

def date_of_next(d)
  Date.today.step(Date.today + 7).select{ |i| puts i if i.strftime("%A") == "#{d}" && i > Date.today} 
end

#4


0  

Would this meet your requirements?

这会满足您的要求吗?

require 'Date'
def date_of_next(early_dow, late_dow)
  early_date = Date.parse(early_dow)
  late_date = Date.parse(late_dow)
  today = Date.today
  if today <= early_date
    early_date
  elsif today <= late_date
    late_date
  else
    early_date + 7
  end
end