Calculating the number of weeks in a year with Ruby

后端 未结 3 1706
抹茶落季
抹茶落季 2020-12-03 18:31

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.

3条回答
  •  無奈伤痛
    2020-12-03 19:11

    You can do the following:

    require 'date'
    @year = 2001 #year you want to count the number of weeks
    d = Date.new @year, 12, 30 # as in Date.new 
    d.cweek # returns the commercial week number for the last week of the year, in this case, 52
    

    if that's what you're looking for :)

    PS: that only works for commercial year though, so in 2001, the 31th of December was actually commercial week 1

提交回复
热议问题