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

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

Asked on December 28, 2018 in Ruby.
Add Comment


  • 12 Answer(s)

    From ActiveSupport , it can be implemented.

    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
    
    Answered on December 28, 2018.
    Add Comment

    Here is the solution,

    The chances are  get hamstrung especially, In case we are working in Rails, or if  we switch among Time, Date, and DateTime, especially when it comes to use or deal with UTC/time zones, daylight savings, and the like.

    It’s best to use Time, and stick with it anywhere.

    We use, there are two good ways, depending on context:

    use the class method on Time If we really want  month m and year y,

    days = Time.days_in_month(m, y)
    
    

    Incase we need  a Time object t, cleaner to ask the day number of the last day of the month, the final month’s dat.

    days = t.end_of_month.day
    
    
    Answered on December 28, 2018.
    Add Comment

    One can use this code for get the number of days in a given month in 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)
    
    Answered on December 28, 2018.
    Add Comment
    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
    Answered on May 20, 2019.
    Add Comment
    def days_of_month(month, year)
      Date.new(year, month, -1).day
    end
    Answered on May 20, 2019.
    Add Comment
    def days_of_month(month, year)
      Date.new(year, month, -1).day
    end
    Answered on May 20, 2019.
    Add Comment

    In PHP, there was an argument you could pass to the Date function to
    get the number of days in the current month:

    echo date(“t”); // Outputs “28” for February

    I don’t see anything like this in Ruby/Rails. Right now, I’m using a
    very ugly line to pull the last day of the month:

    @number_of_days = (Date.strptime(Date.today.strftime(“%Y-%m-01”)) >>

      • 1

    Basically, it takes the first day of the current month, adds one
    month, and then subtracts one day. Am I completely overlooking some
    obvious and easy way to do this? (I’m assuming so).

    Answered on May 20, 2019.
    Add Comment

    In PHP, there was an argument you could pass to the Date function to
    Basically, it takes the first day of the current month, adds one
    month, and then subtracts one day. Am I completely overlooking some
    obvious and easy way to do this? (I’m assuming so).


    Rails mailing list
    [email protected]
    http://lists.rubyonrails.org/mailman/listinfo/rails

    Answered on May 20, 2019.
    Add Comment
    OMMON_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
    Answered on May 20, 2019.
    Add Comment
    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)
    Answered on May 20, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.