Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.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 Rails以月为单位计算日期范围_Ruby On Rails_Ruby_Ruby On Rails 3_Date - Fatal编程技术网

Ruby on rails Rails以月为单位计算日期范围

Ruby on rails Rails以月为单位计算日期范围,ruby-on-rails,ruby,ruby-on-rails-3,date,Ruby On Rails,Ruby,Ruby On Rails 3,Date,如何计算月内两个日期的差值?另外,如果它有区别,我使用的是日期对象,而不是日期时间。此外,一些舍入选项可能很好,这样我就可以控制是否要对部分月份进行舍入 谢谢 此功能有一个rails助手: 这应该给出一个大致值: Date1 - Date2 = difference_in_days (difference_in_days/30).round = difference_in_months 从另一个日期或日期时间中减去一个日期或日期时间将得到天数作为分数,但这可以根据需要作为Float或Fixnu

如何计算月内两个日期的差值?另外,如果它有区别,我使用的是日期对象,而不是日期时间。此外,一些舍入选项可能很好,这样我就可以控制是否要对部分月份进行舍入


谢谢

此功能有一个rails助手:


这应该给出一个大致值:

Date1 - Date2 = difference_in_days
(difference_in_days/30).round = difference_in_months

从另一个日期或日期时间中减去一个日期或日期时间将得到天数作为分数,但这可以根据需要作为
Float
Fixnum
进行评估

例如:

(Date.today - Date.today.advance(:months => -3)).to_f
# => 89.0
从今天到三个月前的同一个日历日期之间有89.0天。如果使用30天-月或平均值30.4375计算,则从那时到现在的时间间隔为2.92个月,或四舍五入到最接近的整数3


如果您想计算日历月的准确数量,这是比较棘手的,但可以做到。

类似的方法比计算秒更易读,并将为您提供实际的日历差异:

# Calculate differnce between two dates in months
# Produces b - a
def month_difference(a, b)
    difference = 0.0
    if a.year != b.year
        difference += 12 * (b.year - a.year)
    end
    difference + b.month - a.month
end

如果你也需要根据天数计算出差异,你可以按照模式来做

我们需要一些类似的东西,但包括部分月份。因此,1/31到2/1仍将产生2个月的收益。也许会有帮助

def self.month_count(range)
  12 * (range.end.year - range.begin.year) + range.end.month - range.begin.month
end

这种做法怎么样

current_date = start_date

while current_date < end_date
  # something  
  current_date = current_date.next_month
end
当前日期=开始日期
当前日期<结束日期
#某物
当前日期=当前日期。下个月
结束

这个答案对聚会来说太晚了,基于之前的答案,可能写得更简洁,但是,它确实给出了两个日期之间的日历差异,并考虑了天数

def difference_in_months(start_date, today)
  date_to_months(today) - date_to_months(start_date) + adjustment_for_days(start_date, today)
end

def date_to_months(date)
  date.year * 12 + date.month
end

def adjustment_for_days(earlier_date, later_date)
  if later_date.day == earlier_date.day
    0
  elsif later_date.day > earlier_date.day
    1
  else
   -1
  end
end

我需要两个日期之间的确切月数(包括小数),并为此编写了以下方法

def months_difference(period_start, period_end)
  period_end = period_end + 1.day
  months = (period_end.year - period_start.year) * 12 + period_end.month - period_start.month - (period_end.day >= period_start.day ? 0 : 1)
  remains = period_end - (period_start + months.month)

  (months + remains/period_end.end_of_month.day).to_f.round(2)
end
如果将9月26日与9月26日(同一天)进行比较,我将其计算为1天。如果不需要,可以删除方法中的第一行:
period\u end=period\u end+1.day

它通过以下规格:

expect(months_difference(Date.new(2017, 8, 1), Date.new(2017, 8, 31))).to eq 1.0
expect(months_difference(Date.new(2017, 8, 1), Date.new(2017, 8, 30))).to eq 0.97
expect(months_difference(Date.new(2017, 8, 1), Date.new(2017, 10, 31))).to eq 3.0
# Overlapping february (28 days) still counts Feb as a full month
expect(months_difference(Date.new(2017, 1, 1), Date.new(2017, 3, 31))).to eq 3.0
expect(months_difference(Date.new(2017, 2, 10), Date.new(2017, 3, 9))).to eq 1.0
# Leap year
expect(months_difference(Date.new(2016, 2, 1), Date.new(2016, 2, 29))).to eq 1.0

它依赖于Rails的ActiveSupport。

我喜欢这一个,但编写代码听起来有点乏味。使用
DateTime#to#u Date
Date#to#u DateTime
方法,DateTime和Date在某种程度上是可互换的。也许,可能会对您有所帮助?日历月将是首选。。。但是如果这太棘手了,我会使用除以30(或30.44)的方法。你知道吗,在仔细考虑了一下,以及我将如何尝试实施日历月之后,我觉得这不值得。我会用你的方法。谢谢你的提醒。。。我正在寻找一个数字,这样我可以用它做一些计算,但这肯定会在其他地方有用!是的,如果日历月的方法太复杂而不值得的话,我可能不得不这么做。你可能想将“轮”改为“地板”,以获得更准确的结果W,谢谢!这是一段有用的代码!看起来你可以通过将方法的前4行替换为
difference=12*(b.year-a.year).abs
(使用.abs)来缩短时间@brettish我会将abs保留在函数的外部,否则函数将丢弃方向信息。我还认为,当a.year>b.year,但a.month