Oracle add_months
function will return the last day of month if you pass the last day of month to it.But in python ,as far as I have known,relativedelta cannot return the last day of month.
I cannot import MonthDelta,and I don't know if it works.
For example,add_months(to_date('20190430','yyyymmdd'),-6)
returns '20181031'
,
from dateutil.relativedelta import relativedelta
from datetime import date
date(2019,4,30) - relativedelta(months=6)
# returns 20181030
I wonder if there is some function acting exactly as add_month
do in python or greenplum.
You can add one day to the initial last day of the month before subtracting months. Then you can subtract that day to get the last day of previous month.
import datetime
import dateutil.relativedelta
my_date = datetime.date(2019,4,30)
date_six_months_ago = my_date + datetime.timedelta(days=1) - dateutil.relativedelta.relativedelta(months=6) - datetime.timedelta(days=1)
print(date_six_months_ago)
Outputs:
2019-10-31
Step by step:
2019-04-30 + 1 day = 2019-05-01
2019-05-01 - 6 months = 2018-11-01
2018-11-01 - 1 day = 2018-10-31