用Ruby计算一年中的周数 [英] Calculating the number of weeks in a year with Ruby

查看:112
本文介绍了用Ruby计算一年中的周数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ruby有一种方法来计算给定年份的星期数(ISO 8601)?我正在使用查找表,我想停止使用它。

解决方案

  def num_weeks(year = Date.today.year)
Date.new(年,12,28).cweek#magick date!
end

long_iso_years =(2000..2400).select {| year | num_weeks(year)== 53}

产生与维基百科


Is there a way in Ruby to calculate the number of weeks(ISO 8601) for a given year? I'm currently using a lookup table and I'd like to stop using it.

解决方案

def num_weeks(year = Date.today.year)
  Date.new(year, 12, 28).cweek # magick date!
end

long_iso_years = (2000..2400).select{|year| num_weeks(year) == 53} 

Yields the same list as wikipedia

这篇关于用Ruby计算一年中的周数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆