Learn and Learn

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

Ruby – Get Ordinal Date – ordinal Method of Date Library – Examples & Explanation

The ordinal() method generates a data object indicating the given ordinal date in Ruby language. In this method, the day and the month must be positive or a negative number but not a zero.

Date.ordinal() Method in Ruby

ordinal() method exists in Standard Date Library of Ruby language. The purpose of this method is to generate the Date object indicating the given ordinal date.

Syntax of the ordinal() Method in Ruby

In date, the syntax of ordinal() method is:

Parameter of ordinal() Method in Ruby

The parameter x must be any valid date object. In this method, day and the month must be a positive or negative number but not a zero. This parameter is required.Note: if the x parameter is not a date object ordinal() method will return an error.

 

Return Value of Date.ordinal()

Date.ordinal() method will return the date object indicating the given calendar date.

 

How to set Format of the ordinal() date in Ruby?


Output
2015-01-14

NOTE: The above code (2015,14) in the ordinal method shows that the 14 showing in the above code represents the 14th day of that year. Similarly, (2015,44) shows the 44th day of that year. See another example below:

 

Ruby ordinal() date Format Example 2


Output
2015-02-13

 

Subtract Days from ordinal() date in Ruby Example 1


Output
2015-12-17

NOTE: In the above code (2015, -15) the -15 shows that 15 days are subtracted from the depth of that year. See another example below:

 

Subtract Days from ordinal() date in Ruby Example 2


Output
2015-11-27
2015-11-17
2015-11-07
2015-10-28

 

Add Days in ordinal() date in Ruby


Output
2015-03-05

 

LearnAndLearn.com © 2018 - All rights reserved