Reputation: 91959
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
Upvotes: 3
Views: 5088
Reputation: 1121524
Use datetime.timedelta()
; simply subtract the two dates:
delta = end_financial_month - from_day
return delta.days
Upvotes: 7
Reputation: 79441
It seems there's some confusion about what a timedelta
is versus a relativedelta
.
A timedelta
is a context-free duration. Think of it as some number of microseconds. When you take a datetime
and add a timedelta
to it, you get a datetime
that number of microseconds in the future, which is defined agnostic to varying month lengths, leap years, etc.
A relativedelta
is a context-sensitive duration. Add "a month" to February 1. Add "a month" to August 1. You'll be adding a different number of microseconds in each case, because "a month" (as a duration) has different meanings depending on the reference point or context. The same thing goes for adding "a year" when leap years are involved.
Adding two timedelta
instances makes perfect sense and is well-defined. You're just adding two "number of microseconds."
Adding two relativedelta
instances isn't so straightforward and, looking at the documentation, appears to be disallowed.
All that being said, Martijn's answer is of course correct. I just wanted to clarify the difference in meaning between these two concepts.
Upvotes: 11