ruby 计算两个日期之间的天数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9572860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 04:52:04  来源:igfitidea点击:

Count number of days between two dates

ruby

提问by andkjaer

How do I count the number of days between these two dates?

如何计算这两个日期之间的天数?

start_date = Date.parse "2012-03-02 14:46:21 +0100"
end_date =  Date.parse "2012-04-02 14:46:21 +0200"

回答by Andrew France

With the Date (and DateTime) classes you can do (end_date - start_date).to_ito get the number of days difference.

使用 Date(和 DateTime)类,您可以(end_date - start_date).to_i获取天数差异。

回答by lee

Assuming that end_dateand start_dateare both of class ActiveSupport::TimeWithZonein Rails, then you can use:

假设end_datestart_date都是ActiveSupport::TimeWithZoneRails 中的类,那么您可以使用:

(end_date.to_date - start_date.to_date).to_i

回答by Justin Herrick

Rails has some built in helpersthat might solve this for you. One thing to keep in mind is that this is part of the Actionview Helpers, so they wont be available directly from the console.

Rails 有一些内置的帮助程序可以为您解决这个问题。要记住的一件事是,这是 Actionview Helpers 的一部分,因此它们不能直接从控制台使用。

Try this

尝试这个

<% start_time =  "2012-03-02 14:46:21 +0100" %>
<% end_time   =  "2012-04-02 14:46:21 +0200" %>
<%= distance_of_time_in_words(start_time, end_time)  %>

 "about 1 month"

回答by thatdankent

I kept getting results in seconds, so this worked for me:

我一直在几秒钟内得到结果,所以这对我有用:

(Time.now - self.created_at) / 86400

回答by a14m

to get the number of days in a time range (just a count of all days)

获取时间范围内的天数(只是所有天的计数)

(start_date..end_date).count
(start_date..end_date).to_a.size
#=> 32

to get the number of days between 2 dates

获取两个日期之间的天数

(start_date...end_date).count
(start_date...end_date).to_a.size
#=> 31

回答by Yuri Ghensev

None of the previous answers (to this date) gives the correct difference in days between two dates.

以前的答案(到这个日期)都没有给出两个日期之间的正确天数差异。

The one that comes closest is by thatdankent. A full answer would convert to_iand then divide:

最接近的是thatdanken。完整的答案将转换to_i然后划分:

(Time.now.to_i - 23.hours.ago.to_i) / 86400
>> 0

(Time.now.to_i - 25.hours.ago.to_i) / 86400
>> 1

(Time.now.to_i - 1.day.ago.to_i) / 86400
>> 1

In the question's specific example, one should not parse to Dateif the time passed is relevant. Use Time.parseinstead.

在问题的具体示例中,Date如果经过的时间相关,则不应解析为。使用Time.parse来代替。

回答by Dorian

To have the number of whole days between two dates (DateTimeobjects):

要获得两个日期(DateTime对象)之间的整天数:

((end_at - start_at).to_f / 1.day).floor

回答by giapnh

To get the number of days difference by two dates:

要获得两个日期相差的天数:

(start.to_date...end.to_date).count - 1
or 
(end.to_date - start.to_date).to_i

回答by Teja

def business_days_between(date1, date2)
  business_days = 0
  date = date2
  while date > date1
   business_days = business_days + 1 unless date.saturday? or date.sunday?
   date = date - 1.day
  end
  business_days
end