Ruby 请教下,如何得到某年某月中有多少天?

d4rkl0rd · December 29, 2011 · Last by ODM replied at February 04, 2012 · 3280 hits

请教下,如何得到某年某月中有多少天? 我想这个应该是现成的方法吧?

Time.days_in_month(12,2011)

class Date
  def self.gregorian_leap?(y)
    y % 4 == 0 && y % 100 != 0 || y % 400 == 0
  end
end

class Time
  COMMON_YEAR_DAYS_IN_MONTH = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  def self.days_in_month(month,year = now.year)
      return 29 if month == 2 && ::Date.gregorian_leap?(year)
      COMMON_YEAR_DAYS_IN_MONTH[month]
  end
end

puts Time.days_in_month(2,2000)
You need to Sign in before reply, if you don't have an account, please Sign up first.