Learn and Learn

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

Ruby – Get commercial Date- commercial() Method with Examples

In Ruby, the commercial() method creates the date object indicating the given week date. In this method, week and day of a week must be a positive or negative number but they must not be a zero.

commercial() Method in Ruby

commercial() Date method exists in Standard Date Library of Ruby. The purpose of this method is to create the date object indicating the given week date.

 

Syntax of the commercial() Method in Ruby

In date library, the syntax of commercial() method is:

 

Parameter of commercial() Date Method in Ruby

The parameter x is any valid date object containing week and day of a week must be a positive or negative number but they must not be a zero. This parameter is required.Note: If the x parameter is not a date object or zero commercial() Date method will return an error.

 

Return Value of commercial() Date Method

commercial() Date method will return the date object indicating the given week date.

 

Ruby – Get the Particular Date with commercial() Date Method


Output
2014-12-29

NOTE: In the above code (2015,1,1) is indicating the year, month, and day respectively. You can see the date in the output as 29, it means that that week started on 29th (Monday) of the previous year. To understand more see examples below.

 

Ruby – Get Week and Year in commercial() Date Method


Output
2015-01-05

NOTE: In the above code (2015, 2, 1) it shows that the second week of the year 2015 is starting on 5th of January 2015. In the output of the above code (2015, 01, 05) the 01 is indicating the indicating the 1st month and 05 is indicate the date of the first week (week is always started on Monday).

 

Ruby – Get the Particular Day in commercial() Date Method

For getting the particular week day the following code is useful.

Output
2017-07-28

 

Understanding of Year, Week and Day in commercial() Date Method of Ruby


Output
2015-01-12
2015-01-19
2015-01-12
2015-01-26
2015-02-09
2015-02-10
2015-02-18

 

LearnAndLearn.com © 2018 - All rights reserved