I have two date fields campaign_start_date
and campaign_end_date
. I want to count the number of days in each month that comes in-between the campaign_start_date
and campaign_end_date
.
eg:
campaign_start_date = September 7 2017
campaign_end_date = November 6 2017
The solution should be :
Total No:of days = 61 days
No: of months = 3 months
Month 1 = 9/7/2017 to 9/30/2017
Month 2 = 10/1/2017 to 10/31/2017
Month 3 = 11/1/2017 to 11/6/2017
No:of days in Month 1 = 24 days
No:of days in Month 2 = 31 days
No:of days in Month 3 = 6 days
How can I achieve this using Python? So far I have achieved:
@api.multi
def print_date(self):
start_date = datetime.strptime(self.start_date, "%Y-%m-%d %H:%M:%S")
end_date = datetime.strptime(self.end_date, "%Y-%m-%d %H:%M:%S")
campaign_start_date = date(start_date.year,start_date.month,start_date.day)
campaign_end_date = date(end_date.year,end_date.month,end_date.day)
duration = (campaign_end_date-campaign_start_date).days
return True
Calculate the duration in days:
Some hints for further calculations:
This way you can calculate the number of days in each month of the campaign (keep in mind to check if
campaign_end_date
is in another month thancampaign_start_date
For calculations you can also access the fields of a date, e.g.
To calculate the number of involved month in your campaign and to get a list of the month to calculate the duration per month you can use this (based on the answer of m.antkowicz in Python: get all months in range?). It's important to set the day to 1 (
current = current.replace(day=1)
) before and inside the loop, otherwise you skip a month when your startdate is 31st of a month and the next month is shorter than 31 days or if you have a longer period:which prints (when you use
current.strftime('%Y-%m-%d')
:now you can loop over the result list and calculate the number of days per months:
which gives you the desired "No:of days in Month x" as a list: