Looking for a best way to get month with last day during adding operation.
For examle: I am getting last day of Movember and I need to add 1 month from this day, 1 full calendar month:
2.6.3 :002 > date = Date.strptime('November 30, 2020', '%B %d, %Y')
=> Mon, 30 Nov 2020
2.6.3 :003 > date + 1.month
=> Wed, 30 Dec 2020
This is wrong for me, because it will be 31 days in December.
Another example:
2.6.3 :005 > date2 = Date.strptime('February 29, 2020', '%B %d, %Y')
=> Sat, 29 Feb 2020
2.6.3 :006 > date2 + 1.month
=> Sun, 29 Mar 2020
I am expecting 31 of Mar instead of 29
For now - I know only one solution - something like:
2.6.3 :017 > (date + 1.month).end_of_month
=> Thu, 31 Dec 2020
2.6.3 :018 > (date2 + 1.month).end_of_month
=> Tue, 31 Mar 2020
Is it the correct and best way?