Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/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 on rails 3 Rails每月的周数_Ruby On Rails 3_Date - Fatal编程技术网

Ruby on rails 3 Rails每月的周数

Ruby on rails 3 Rails每月的周数,ruby-on-rails-3,date,Ruby On Rails 3,Date,在rails中,如何计算给定月份的周数 谢谢我不知道你到底想要什么。。。但也许你想要这样的东西: (时间:月内天数(2010年5月)。至7日) #>4.42857142857143我需要知道一个月有多少周,包括部分周。把它想象成日历中的行。你需要多少行?你必须考虑每天的天数和月开始的日期。例如,2011年10月实际上有6个独特的星期 这是我的答案(@date是当前日期): 您可以使用以下方法: WEEK_NUMBER_FORMAT = '%W' # Returns the first

在rails中,如何计算给定月份的周数


谢谢

我不知道你到底想要什么。。。但也许你想要这样的东西:

(时间:月内天数(2010年5月)。至7日)


#>4.42857142857143

我需要知道一个月有多少周,包括部分周。把它想象成日历中的行。你需要多少行?你必须考虑每天的天数和月开始的日期。例如,2011年10月实际上有6个独特的星期

这是我的答案(@date是当前日期):


您可以使用以下方法:

  WEEK_NUMBER_FORMAT = '%W'

  # Returns the first day of month.
  # If invoked without any arguments, this would return the
  # first day of current month
  def first_day_of_month(date_time=Time.now)
    date_time.beginning_of_month
  end

  # Returns the last day of month.
  # If invoked without any arguments, this would return the
  # last day of current month
  def last_day_of_month(date_time=Time.now)
    date_time.end_of_month
  end

  # Returns the week number in the year in which the specified date_time lies.
  # If invoked without any arguments, this would return the
  # the week number in the current year
  def week_number(date_time=Time.now)
    date_time.strftime(WEEK_NUMBER_FORMAT).to_i
  end

  # Returns the number of weeks in the month in which the specified date_time lies.
  # If invoked without any arguments, this would return the
  # the number of weeks in the current month
  def weeks_in_month(date_time=Time.now)
    week_number(last_day_of_month(date_time)) - week_number(first_day_of_month(date_time))  + 1
  end
用法:周/月(日期/时间)

希望有帮助:

谢谢

Jignesh使用宝石

获取这样的月份的周数

number_of_weeks_month(Date.parse("2017-11-01"),0,Date.parse("2017-11-30"))

这份报告4

嗨,迈克尔,我想计算一下从周日到周一,一月、二月等有多少周?因为否则它总是4。28/7是4。30/7=4.29等。我不明白,有些蛾子有5周,而有些蛾子有4周,对吗?我能计算一下吗?一个月的最大天数是31天。31/7=4.43周。所以一个月内不可能有5个完整的星期。好吧,愚蠢的我!但是有没有办法知道一个月有多少周呢?这很简单,就像
(从..到.)。每个切片(7).到..大小一样。这是如何在您的gem存储库中声明的。添加这样的答案看起来就像升级自己的存储库。
def number_of_weeks_in_month
  4
end
def number_of_weeks_month(start_of_month, count, end_of_month)
 if start_date > end_of_month
  return count
 else
  number_of_weeks_month(start_date.end_of_week + 1, count + 1, end_of_month)
 end
end
number_of_weeks_month(Date.parse("2017-11-01"),0,Date.parse("2017-11-30"))
def number_of_weeks_in_month
  4
end
Date.today.end_of_month.day/7
=> 4