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

d4rkl0rd · 2011年12月29日 · 最后由 ODM 回复于 2012年02月04日 · 3280 次阅读

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

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)
需要 登录 后方可回复, 如果你还没有账号请 注册新账号