Count number of days between two dates

Ruby

Ruby Problem Overview


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"

Ruby Solutions


Solution 1 - Ruby

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

Solution 2 - Ruby

Assuming that end_date and start_date are both of class ActiveSupport::TimeWithZone in Rails, then you can use:

(end_date.to_date - start_date.to_date).to_i

Solution 3 - Ruby

Rails has some built in helpers that 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.

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"

Solution 4 - Ruby

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

(Time.now - self.created_at) / 86400

Solution 5 - Ruby

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

Solution 6 - Ruby

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_i and then divide:

(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 Date if the time passed is relevant. Use Time.parse instead.

Solution 7 - Ruby

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

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

Solution 8 - Ruby

Very late, but it may help others:

end_date.mjd - start_date.mjd

https://apidock.com/ruby/Date/mjd

Solution 9 - Ruby

The best solution currently that I've seen work consistently well is using the following pattern:

(end_date.beginning_of_day - Time.now.utc.beginning_of_day).seconds.in_days

Solution 10 - Ruby

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

Solution 11 - Ruby

(end_date - start_date)/1000/60/60/24

any one have best practice please comment below

Solution 12 - Ruby

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionandkjaerView Question on Stackoverflow
Solution 1 - RubyAndrew FranceView Answer on Stackoverflow
Solution 2 - RubyleeView Answer on Stackoverflow
Solution 3 - RubyJustin HerrickView Answer on Stackoverflow
Solution 4 - RubythatdankentView Answer on Stackoverflow
Solution 5 - Rubya14mView Answer on Stackoverflow
Solution 6 - RubyYuri GhensevView Answer on Stackoverflow
Solution 7 - RubyDorianView Answer on Stackoverflow
Solution 8 - RubyAaqibView Answer on Stackoverflow
Solution 9 - RubyibrahimabView Answer on Stackoverflow
Solution 10 - RubygiapnhView Answer on Stackoverflow
Solution 11 - RubyAlmokhtarView Answer on Stackoverflow
Solution 12 - RubyTejaView Answer on Stackoverflow