Learn and Learn

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

Ruby – Find Previous Year – prev_year Method – Examples & Explanation

In Ruby prev_year is used to get the previous year from the year given in a code. For example, if in your code, you have entered the year 2014 and with the help of the prev_year method you will find 2013. You can also get the desired previous year of the year given in a code with the help of this method of Ruby.

Syntax of prev_year Method in Ruby

Date.new(x).prev_year

Parameter of prev_year Method in Ruby

x – where x is any valid date object such as 2014, 5, 14.
This parameter is required.

Error Handling of prev_year Method in Ruby

If the x parameter is not a valid date object the prev_year method will return an error and it could be due to the wrong format of the date in a code or the use of zero.

Return Value of prev_year Method in Ruby

prev_year method will return previous year from the year given in a code. You can also get desired previous year by using this method of Ruby.

Ruby prev_year Method Example


Output:
2013,4,5

Ruby Different Methods of prev_year


Output:
2013-04-05T11:20:15+00:00
2012-04-05T11:20:15+00:00

How to Print Previous Year from Current Year in Ruby?


Output:
2011-04-05
2009-04-05

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