Soutenez Time.now : Rejoignez notre Plan Premium pour une expérience sans publicité ! Soutenez-nous : Passez Premium sans pub !

Week numbers in Ruby

To get the current ISO 8601 week number in Ruby, use the following code snippet:

require "date" puts Date.today.cweek

* Ensure your system or application regional settings are set to follow ISO 8601 standards (weeks start on Monday) if the output varies.

Instructions étape par étape

  1. Require the date library: require "date".
  2. Call Date.today.cweek for the ISO week number.
  3. Use Date.today.cwyear for the matching ISO year.

Bon à savoir

cweek/cwyear are the ISO 8601 pair. Date#strftime("%V") gives the same week number as a zero-padded string; avoid %U and %W, which use different week-start rules.