Reputation: 69
I need to change months from a string value to a integer value so I can perform a calculation. I am using the datetime library which can give the current date and I need to compare this to a date entered by the user to find the difference between the months in integer form.
import datetime
current_month = datetime.date.today().strftime('%B')
month_join = input('Please enter the month you joined')
month_difference = current_month - month_join
I would like the input to be as a month if possible. If not I will just use:
month_join = int(input('Please enter the month you joined')
Upvotes: 1
Views: 420
Reputation: 111
Try the monthdelta library.
import datetime
import monthdelta
current_month = datetime.date.today()
year = current_month.year
month_in = input('Please enter the month you joined')
month_dt = datetime.datetime.strptime(month_in, '%B').date()
# Construct new datetime object with current year
month_join = datetime.date(year=year, month=month_dt.month, day=1)
# Reduce year by one if the month occurs later
if month_join > current_month:
month_join = datetime.date(year=year - 1, month=month_dt.month, day=1)
month_difference = monthdelta.monthmod(month_join, current_month)
print(month_difference[0].months)
Upvotes: 0
Reputation: 868
This method allows the user multiple chances to enter a correct answer if they mess up the first time. It also checks to make sure that the number is a valid month. You can also add checks to see if the user needs to include the year. IE: if current_month - month_join < 0
ask them for the year.
import datetime
current_month = datetime.date.today().month
month_join = None
while month_join is None:
month_join = raw_input('Please enter the month you joined: ')
if month_join == 'quit':
exit(1)
try:
month_join = int(month_join)
if month_join > 12 or month_join < 1 or not isinstance(month_join, int):
print('Please enter a month value that is from 1 to 12')
month_join = None
except Exception as e:
if isinstance(e, TypeError) or isinstance(e, ValueError):
print('Please enter the month as an integer. IE. May = 5. If you want to close the program type "quit"')
month_join = None
else:
raise e
month_difference = current_month - month_join
print month_difference
Upvotes: 1
Reputation: 1443
It sounds like what you need is a dictionary which relates the name of a month and its numerical value.
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])
print(month_difference)
You can also accomplish the creation of the dictionary through use of the calendar
module's month_name
list attribute.
import datetime, calendar
months = {name : (index + 1) for index, name in enumerate(calendar.month_name[1:])}
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])
print(month_difference)
Upvotes: 1
Reputation: 2510
Probably the strptime method in datetime will be of some use to you. For example:
import datetime
current_month = datetime.date.today().strftime('%B')
month_join = datetime.datetime.strptime(input('Please enter the month you joined'), "%B")
month_difference = current_month.month - month_join.month
But be careful with this - what if the user joined in the previous calendar year? You would end up with a negative value for month_difference.
You would probably be better off actually getting the full month and year the user joined, turn that into a datetime object, subtract it form datetime.today() to get a timedelta object. Then get the month count from that timedelta object.
You may as well leverage the datetime library as much as possible, rather than trying to reinvent the wheel.
Upvotes: 1