Learn and Learn

Learn And Learn - great place for tutorials, references and how-to

Ruby – Find Calendar Week Number – cweek Method with Examples

cweek method is used to find the calendar week number starting from 1 to maximum 53 weeks. cweek method is the easiest way to get the number of a week from the given calendar year than any other method in Ruby. cweek is a part of Date Library in Ruby.

Syntax of cweek Method in Ruby

The syntax of cweek method in Ruby Language is:

 

Parameters of cweek Method in Ruby

x – Where x is any valid date object from which, it is required to get the number 0f a week. This parameter is required.

 

Error Handling

If the x parameter is not a valid date object the cweek method returns an error.Note: If the x parameter is zero, this shows the number of a week as ‘1’ but if it contains only the year like 2012 or 2016 then the output of that code will be 52 and 53 respectively according to the total number of the weeks in that particular year.

 

Return Value of cweek Method in Ruby

cweek method will return a number of the week in a calander year.

 

Ruby – Use of cweek


Output:
53

How to get the number of a week in Ruby?


Output:
43

Get the Number of a calander week with cweek Method in Ruby


Output:
2

LearnAndLearn.com © 2018 - All rights reserved