Learn and Learn

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

Ruby – Find Calendar Week Based Year – cwyear Method with Examples

cwyear method is used to returns the calendar week based year. In this method, all weeks of the previous year must have been passed before getting the first week of the new calendar year. For example 2014, 1, 1 seems new calendar year but actually it still carrying the last week of the previous year. You will know better about this week based year in the following lines. cwyear is a part of Date Library in Ruby.

Syntax of cwyear Method in Ruby

The syntax of cwyear method in Ruby Language is:


Parameters of cwyear Method in Ruby

x – Where x is any valid date object out of which the week based year is required. This parameter is required.


Error Handling

If the x parameter is not a valid date object the cwyear method returns an error.


Return Value of cwyear Method in Ruby

cwyear method will return a number according to calendar week based year.


Ruby – Use of cwyear


Note: As shown in the above code, that the year is 2012 and output coming as 2011 main reason behind it is that the code still carrying the 52nd week of 2011. This code will show 2012 when the date changes to 2012, 6, 1 which is the first week of 2012.

How to Get the Calendar Week based year with cwyear Method in Ruby?


Ruby – cwyear Use and Example


LearnAndLearn.com © 2018 - All rights reserved