How to get the number of days in a given month in Ruby, accounting for year?
RubyDateRuby Problem Overview
I'm sure there's a good simple elegant one-liner in Ruby to give you the number of days in a given month, accounting for year, such as "February 1997". What is it?
Ruby Solutions
Solution 1 - Ruby
If you're working in Rails, chances are you'll get hamstrung eventually if you switch among Time
, Date
, and DateTime
, especially when it comes to dealing with UTC/time zones, daylight savings, and the like. My experience has been it's best to use Time
, and stick with it everywhere.
So, assuming you're using Rails's Time class, there are two good options, depending on context:
-
If you have a month
m
and yeary
, use the class method onTime
:days = Time.days_in_month(m, y)
-
If you have a Time object
t
, cleaner to ask the day number of the last day of the month:days = t.end_of_month.day
Solution 2 - Ruby
require 'date'
def days_in_month(year, month)
Date.new(year, month, -1).day
end
# print number of days in February 2012
puts days_in_month(2012, 2)
Solution 3 - Ruby
This is the implementation from ActiveSupport (a little adapted):
COMMON_YEAR_DAYS_IN_MONTH = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
def days_in_month(month, year = Time.now.year)
return 29 if month == 2 && Date.gregorian_leap?(year)
COMMON_YEAR_DAYS_IN_MONTH[month]
end
Solution 4 - Ruby
How about:
require 'date'
def days_in_month(year, month)
(Date.new(year, 12, 31) << (12-month)).day
end
# print number of days in Feburary 2009
puts days_in_month(2009, 2)
You may also want to look at Time::days_in_month in Ruby on Rails.
Solution 5 - Ruby
In Rails project for current date
Time.days_in_month(Time.now.month, Time.now.year)
For any date t which is instance of Time
Time.days_in_month(t.month, t.year)
or
t.end_of_month.day
.
If you have UTC seconds, you need to get an instance of Time first
Time.at(seconds).end_of_month.day
Solution 6 - Ruby
Use Time.days_in_month(month)
where month = 1
for January, 2
for Feb, etc.
Solution 7 - Ruby
For a given Date object I feel like the easiest is:
Date.today.all_month.count
Solution 8 - Ruby
revise the input for other format
def days_in_a_month(date = "2012-02-01")
date.to_date.end_of_month.day
end
Solution 9 - Ruby
as of rails 3.2... there's a built in version: http://api.rubyonrails.org/classes/Time.html#method-c-days_in_month
(alas, it shows up after this answer, which takes folks on a long hike)
Solution 10 - Ruby
Time.now.end_of_month.day
- for current month
Date.parse("2014-07-01").end_of_month.day
- use date of first day in month.
Depends on ActiveSupport
Solution 11 - Ruby
A simple way using Date
:
def days_of_month(month, year)
Date.new(year, month, -1).day
end
Solution 12 - Ruby
I think it's the simplest way to get it
def get_number_of_days(date = Date.today)
Date.new(date.year, date.month, -1).mday
end
Solution 13 - Ruby
Since the time is irrelevant for this purpose then you just need a date object:
[*Date.today.all_month].size