Search code examples
pythonpython-dateutil

how to convert months into days when using relativedelta?


Here is my method

def get_remaining_days_in_financial_month(self, from_day):
    current_financial_day = date(from_day.year, from_day.month,
                                 self.financial_day_of_month)
    end_financial_month = current_financial_day + relativedelta(months=+1)
    delta = relativedelta(end_financial_month, from_day)
    remaining_days_in_financial_month = delta.days
    return remaining_days_in_financial_month

When I debug, I see

current_financial_day = 2013-06-01
delta = relativedelta(months=+1)
end_financial_month = 2013-07-01
from_day = 2013-06-01
remaining_days_in_financial_month = 0

Although this information is correct, I would like to know the number of days, because number of days changes from 28 in Feb, to 30 in June and 31 in August

How can I achieve this? The dateutil library doesn't offer a way it seems

Thank you


Solution

  • Use datetime.timedelta(); simply subtract the two dates:

    delta = end_financial_month - from_day
    return delta.days