In Ruby next_month
is used to get one month ahead of the month given in a code. For example, if in your code, you have entered the fourth month and with the help of the next_month
method, you will find the fifth month of that particular year. You can also get the desired month from the given code with the help of this method of Ruby.
Syntax of next_month
Method in Ruby
Date.new(x).next_month
Parameter of next_month
Method in Ruby
x
– where x
is any valid date object such as 2016, 5, 10.This parameter is required.
Error Handling of next_month
Method in Ruby
If the
x
parameter is not a valid date object the next_month
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_month
Method in Ruby
next_month
method will return one month ahead of the month given in a code. You can also get desired month by using this method of Ruby.Ruby next_month
Method Example
# import date library require 'date' x = Date.new(2016,5,10).next_month puts x
Output:
2016,6,10
Ruby Different Methods of next_month
# import date library require 'date' x = DateTime.new(2014,5,15, 10, 22, 55).next_month x = DateTime.new(2014,5,15, 10, 22, 55).next_month(2) puts x
Output:
2014-06-15T10:22:55+00:00
2014-07-15T10:22:55+00:00
How to Print Next Month from Current Month in Ruby?
# import date library require 'date' # you can get desired month by adding months after next_month x = Date.new(2014,9,15).next_month(3) x = Date.new(2014,9,15).next_month(6) puts x
Output:
2014-12-15
2015-03-15