Learn and Learn

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

Ruby – new Date Method of Date Library – Examples & Explanation

The new() method creates the date object indicating the given calendar date in Ruby language. 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 the year -1. In this method, the day and the month must be a positive or negative number but not a zero.

Date.new() Method in Ruby

new() 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 new() Method in Ruby

In date, the syntax of new method is:

 

Parameter of new() Method in Ruby

The parameter x is 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 or zero new() method will return an error.

 

Return Value of Date.new()

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

 

How to Add Date and Time in new() date method of Ruby?


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

 

How to Add Days in new() date method in Ruby?


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

 

Get the Output of new() date method in string


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

 

Getting the Particular Year with new() date method in Ruby


Output
2014-01-01

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

 

Getting Particular Date in new() method


Output
2014-11-25

 

Get Inverse Date with new() date method in Ruby


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

Note: in the above code -1 means 1 day is subtracted from the depth of a given month.

 

LearnAndLearn.com © 2018 - All rights reserved