Learn and Learn

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

Ruby – Find object indicating given calendar date – civil() Method with Examples

The civil method creates the date object indicating the given calendar date. In this method, BCE years are calculated astronomically. That means, the year is 0 before the year 1 and the year before the 0 year is year -1. In this method the day and the month must be a positive of negative number but not a zero.

civil Date Method in Ruby

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

 

Syntax of the civil Date Method in Ruby

In date, the syntax of civil method is:

 

Parameter of civil Date Method in Ruby

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

 

Return Value of civil Date Method

civil Date method will return the date object indicating the given calendar date.

 

Ruby – Get Date and Time in civil Date Method


Output
2014-11-25T14:35:00+00:00

 

Add Days in civil Date Method


Output
2014-11-30T14:35:00+00:00

 

Converting the Value to string in civil Date Method


Output
“2014-11-25T00:00:00+00:00”

 

Ruby – Denoting the Particular Year and Date in civil Date Method Example 1


Output
2014-01-01

NOTE: In this method, denoting the year 2014 means the start of the year like 01-01-2014.

 

Ruby – Denoting the Particular Year and Date in civil Date Method Example 2


Output
2014-11-25

 

How to Get the Inverse Date in civil Date Method


Output

2014-11-30
2014-11-29
2014-11-28

NOTE: In the above code -1, -2 and -3 are indicating the subtraction of days from the depth of the given month.

 

Loading...
LearnAndLearn.com © 2018 - All rights reserved