Ruby add calendar month

I am new to ruby ​​and have a problem with some date functions. I am trying to add a calendar month to the delivery date, so "April 30, 2002" will be released on "May 31, 2002." Here is my code

 date = '30th Apr 2002'
 parseDate = Date.parse(date)

(parseDate >> 1) # This returns 2002-05-30

Perhaps this does not mean that the function should work, and in this case I will need to write some code to return the last day of the next month, if the provided date is the last day of the month?

Any help would be greatly appreciated.

+5
source share
2 answers

>> just increases the month and keeps the same day for a month, which, as Skeet noted in the comment, is somewhat reasonable ...

, , :

((parseDate +1) >> 1) - 1

, , , , .

((parseDate +1) << 1) - 1
+4

parseDate = parseDate + 1.month.since(parseDate)

30 ,

parseDate = parseDate + (30*24*60*60)
0

All Articles