Learn and Learn

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

Ruby – Find Previous Day – prev_day Method – Examples & Explanation

In Ruby prev_day is used to get one day less than the day given in a code. For example, if in your code, you have entered the tenth day of the fifth month then with the help of the prev_day method you will find the ninth day of that particular month. You can also get the desired day less than the day given in a code with the help of this method of Ruby.

Syntax of prev_day Method in Ruby

Date.new(x).prev_day

Parameter of prev_day Method in Ruby

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

Error Handling of prev_day Method in Ruby

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

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

Ruby prev_day Method Example


Output:
2016-05-09

Ruby Various Methods of prev_day


Output:
2016-04-04T11:20:15+00:00
2016-04-03T12:20:30+00:00

How to Print Previous Day from Current Day in Ruby?


Output:
2016-04-02
2016-04-15

LearnAndLearn.com © 2018 - All rights reserved