Learn and Learn

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

Ruby – Find Previous Month – prev_month Method with Examples

Unlike next month method, the prev_month method is used to get one month less than the month given in a code. For example, if in your code, you have entered the fourth month of the year 2016 and with the help of prev_month method, you will find the third month of that year. You can also get the desired month less than the month given in a code with the help of this method of Ruby.

Syntax of prev_month Method in Ruby

Parameter of prev_month Method in Ruby

x – where x is any valid date object such as 2016, 4, 10. This parameter is required.

Error Handling of prev_month Method in Ruby

If the x parameter is not a valid date object the prev_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 prev_month Method in Ruby

prev_month method will return one month less than the month given in a code. You can also get desired month by using this method of Ruby.

Ruby prev_month Method Example


Output:
2016,3,10

Ruby Different Methods of prev_month


Output:
2014-05-10T11:20:15+00:00
2014-04-10T11:20:15+00:00

How to Print Previous Month from Current Month in Ruby?


Output:
2016-08-25
2016-04-25

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