Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Ruby查找一个月中的任何特定日期_Ruby - Fatal编程技术网

使用Ruby查找一个月中的任何特定日期

使用Ruby查找一个月中的任何特定日期,ruby,Ruby,我正在尝试在我的应用程序中内置日历系统。我现在的问题是每月一次。基本上,如果一个人安排在一个月的第二个星期三,我想自动更新计划到下个月的第二个星期三。我试着只添加+1.month,但这并没有达到我想要的效果。因为它会移动到下个月的日期,而不是一周中的某一天。有人知道我怎样才能做到这一点吗。以下是我目前正在做的不正确的事情 def call schedule.update!( end_at: end_at_next_month, start_at: start_at

我正在尝试在我的应用程序中内置日历系统。我现在的问题是每月一次。基本上,如果一个人安排在一个月的第二个星期三,我想自动更新计划到下个月的第二个星期三。我试着只添加
+1.month
,但这并没有达到我想要的效果。因为它会移动到下个月的日期,而不是一周中的某一天。有人知道我怎样才能做到这一点吗。以下是我目前正在做的不正确的事情

def call
    schedule.update!(
      end_at: end_at_next_month,
      start_at: start_at_next_month,
      status: "monthly",
      arranged: true
    )
  end

  private

  def schedule
    context.schedule
  end

  def end_at_next_month
    schedule.end_at + 1.month
  end

  def start_at_next_month
    schedule.start_at + 1.month
  end
end

你的标题与正文中的解释不同。以我的例子来说,你不会得到最后一个星期五,但你可以选择一个月的1、2、3、4、5个星期三(或任何其他日子)

require 'date'
class Date

  #~ class DateError < ArgumentError; end

  #Get the third monday in march 2008: new_by_mday( 2008, 3, 1, 3)
  #
  #Based on http://forum.ruby-portal.de/viewtopic.php?f=1&t=6157
  def self.new_by_mday(year, month, weekday, nr)

    raise( ArgumentError, "No number for weekday/nr") unless weekday.respond_to?(:between?) and nr.respond_to?(:between?)
    raise( ArgumentError, "Number not in Range 1..5: #{nr}") unless nr.between?(1,5)
    raise( ArgumentError,  "Weekday not between 0 (Sunday)and 6 (Saturday): #{nr}") unless weekday.between?(0,6)

    day =  (weekday-Date.new(year, month, 1).wday)%7 + (nr-1)*7 + 1

    if nr == 5
      lastday = (Date.new(year, (month)%12+1, 1)-1).day # each december has the same no. of days
      raise "There are not 5 weekdays with number #{weekday} in month #{month}" if day > lastday
    end

  Date.new(year, month, day)
  end
end

#2nd monday (weekday 1) in july
p Date.new_by_mday(2017,7,1,2)
需要“日期”
上课日期
#~class DateErrorlastday,则提高“月#{month}中没有5个工作日的编号为#{weekday}”
结束
日期。新(年、月、日)
结束
结束
#7月的第二个星期一(工作日1)
p日期。新的日期(2017年7月1日和2日)

如果您正在寻找gem:支持此功能。

您可以尝试以下方法:

DAYS_MAPPING = { 0=>"Sunday",
  1=>"Monday",
  2=>"Tuesday",
  3=>"Wednesday",
  4=>"Thursday",
  5=>"Friday",
  6=>"Saturday" }


# Returns week of month in integer
#   for example: 1 for matching day of 1st week of the month
def get_week_number(date)
  week_number = date.strftime('%U').to_i - date.beginning_of_month.strftime('%U').to_i
  offset = (Date.parse(DAYS_MAPPING[date.wday]) < date.beginning_of_month) ? 0 : 1

  week_number + offset
end

def specific_day_of_next_month(schedule)
  # wday => day of week in integer
  #   for example: 0 for Sunday; 1 for Monday & so on ...
  wday = schedule.wday
  week_number = get_week_number(schedule)

  next_month = schedule + 1.month
  days = (next_month.beginning_of_month..next_month.end_of_month).select { |d| d.wday == wday }
  days[week_number-1]
end
示例2:

schedule = Date.today
# => Sat, 01 Jul 2017 (First Saturday of July)

specific_day_of_next_month(schedule)
# => Sat, 05 Aug 2017 (First Saturday of August)
schedule = Date.new(2017, 7, 10)
# => Mon, 10 Jul 2017 (Second Monday of July)

specific_day_of_next_month(schedule)
# => Mon, 14 Aug 2017 (Second Monday of August)
获取2017年8月第二个星期日的日期

date_of_nth_wday(8, 2017, 0, 2)
  #=> #<Date: 2017-08-13 ((2457979j,0s,0n),+0s,2299161j)>
date_of_nth_wday(8, 2017, 5, 3)
  #=> #<Date: 2017-08-18 ((2457984j,0s,0n),+0s,2299161j)>
date_of_nth_wday(8, 2017, 1, 5)
  #=> nil (there are only 4 Mondays in August, 2017)

@对不起,伙计。但我的解决方案在某些情况下给出了错误的结果。例如下个月的具体日期(新日期(2017年7月10日))。刚刚更新了答案,
get\u week\u number(date)
方法。感谢您的更新。但是我现在遇到了这个错误。
未初始化常量nextmonthyAssignment::DAYS
知道我需要做什么来修复它吗?我想问它的更好方法是。“DAYS常量试图做什么?”按位更新了答案。
date_of_nth_wday(8, 2017, 1, 5)
  #=> nil (there are only 4 Mondays in August, 2017)