Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.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,如何找到一周或一个月内的日期 days_for_week should return 19,20,21 (assuming current date is 21st) days_for_month should return 1..21 (assuming current date is 21st) 首先,您可以使用Time.now.wday获取当前的工作日,然后减去该值将得到本周的开始日期 第二个更简单,每个月从第一个开始,对吗?假设我正确地阅读了你的问题 第二点很简单: def days_

如何找到一周或一个月内的日期

days_for_week should return 19,20,21 (assuming current date is 21st)
days_for_month should return 1..21 (assuming current date is 21st)

首先,您可以使用
Time.now.wday
获取当前的工作日,然后减去该值将得到本周的开始日期


第二个更简单,每个月从第一个开始,对吗?

假设我正确地阅读了你的问题

第二点很简单:

def days_for_month
  1..Date.today.day
end
第一个需要一个小算法才能回到星期六:

def days_for_week
  days = []
  day = Date.today
  until day.saturday?
    days.unshift(day.day)
    day -= 1
  end
  days
end

主动支持提供了许多有用的方法,如在月初等

> Date.today.at_beginning_of_week
=> Mon, 20 May 2013
对于这种特殊情况,您可以

> (Date.today.at_beginning_of_week..Date.today).map &:day
=> [20, 21]
同样地

> (Date.today.at_beginning_of_month..Date.today).map &:day
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
或者干脆

> 1..Date.today.day