Reputation: 69
If I try to compare dates in the form "Month Year" and the year is different, the month difference won't be correct. If I compare January 2016 to March 2017 the month difference will be 3 and not 14. Is there a way I can put months to 0 when the year increases.
import datetime
month_names = ["January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"]
months = {name : (index + 1) for index, name in enumerate(month_names)}
current_month = datetime.date.today().strftime('%B')
month_joined = input("Please enter the month you joined: ")
month_difference = abs(months[current_month] - months[month_joined])
current_year = int(datetime.date.today().strftime('%Y'))
year_joined = int(input('Please enter the year you joined: '))
year_due = year_joined + 1
year_difference = current_year - year_due
The month difference does not take into accound the difference in years. I have tried a few solutions to ignore an input for month difference and put 12 months in for each year. I need the month difference to be exact. Any ideas? I'm stumped.
Upvotes: 0
Views: 71
Reputation: 2829
I'm not sure what you mean by 'put months to 0' but you can add year_difference * 12
to month_difference
to map March 2017 to Jan 2016 as described.
Upvotes: 1