How to get the number of days in a given month in Ruby for a year?

I'm sure Ruby has a simple, simple, elegant liner to give you the number of days in a given month that make up a year, like February 1997. What is it?

+42
date ruby
Sep 28 '09 at 23:18
source share
11 answers

This is an implementation of ActiveSupport (slightly 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 
+20
Sep 29 '09 at 0:09
source share

If you work in Rails, most likely you will come across in the end if you switch to Time , Date and DateTime , especially when it comes to UTC / time zones, daylight saving time, and the like. My experience was best to use Time and stick with it everywhere.

So, if you use the Rails Time class, there are two good options, depending on the context:

  • If you have month m and year y , use the Time class method:

     days = Time.days_in_month(m, y) 
  • If you have an object of time t , the cleaner will ask for the day number of the last day of the month:

     days = t.end_of_month.day 
+106
May 17 '12 at 17:40
source share
 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) 
+47
Nov 15 '12 at
source share

What 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 can also watch Time :: days_in_month in Ruby on Rails.

+24
Sep 29 '09 at 0:14
source share

In a Rails project for the current date

 Time.days_in_month(Time.now.month, Time.now.year) 

For any date t that is an 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 
+7
Mar 04 '15 at 13:58
source share

Use Time.days_in_month(month) where month = 1 for January, 2 for February, etc.

+5
Sep 02 '12 at 15:04
source share

change input for another format

 def days_in_a_month(date = "2012-02-01") date.to_date.end_of_month.day end 
+5
Apr 11 '13 at 9:43
source share

on rails 3.2 ... there is a built-in version: http://api.rubyonrails.org/classes/Time.html#method-c-days_in_month

(alas, he appears after this answer, which takes people on a long trip)

+3
Jun 04 '13 at 10:22
source share

Time.now.end_of_month.day - for the current month

Date.parse("2014-07-01").end_of_month.day - use the date of the first day in the month.

Depends on ActiveSupport

+3
Feb 11 '15 at 21:19
source share

I think this is the easiest way to get it

 def get_number_of_days(date = Date.today) Date.new(date.year, date.month, -1).mday end 
+1
Mar 22 '14 at 8:53
source share

A simple way to use Date :

 def days_of_month(month, year) Date.new(year, month, -1).day end 
+1
Apr 6 '17 at 13:58 on
source share



All Articles