Learn and Learn

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

Ruby – Find Next Year – next_year() Method – Examples & Explanation

In Ruby next_year() is used to get one year ahead of the year given in a code. For example, if in your code, you have entered the year 2014 and with the help of the next_year() method, you will find 2015. You can also get the desired years ahead of the year given in a code with the help of this method of Ruby.

Syntax of next_year() Method in Ruby

Date.new(x).next_year

Parameter of next_year() Method in Ruby

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

Error Handling of next_year() Method in Ruby

If the x parameter is not a valid date object the next_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 next_year() Method in Ruby

next_year() method will return one year ahead of the year given in a code. You can also get desired year by using this method of Ruby.

Ruby next_year() Method Example


Output:
2015,4,5

Ruby Different Methods of next_year()


Output:
2015-04-05T11:20:15+00:00
2016-04-05T11:20:15+00:00

How to Print Next Year from Current Year in Ruby?


Output:
2017-04-05
2019-04-05

LearnAndLearn.com © 2018 - All rights reserved