I am writing a function to ask the user for a date. I would like the user to be able to enter the date in a familiar format: MM-DD-YYYY
I've tried a number of variations but have been unsuccessful in being able to calculate the difference in days in a delta calculation. My attempts have returned a number of errors, mainly that I cannot pass str arguments while trying to calculate a difference.
What is the best way to get a date input in MM-DD-YYYY while subtracting .today() which returns YYYY-MM-DD, ultimately returning the number of days between the two dates?
Thanks for any and all input! (using .PY 3.6)
Note: the code below DOES work but does not allow for the U.S. date formatting convention.
def get_days_since_last_app():
then = datetime.strptime(input("What is the date of your last credit "
"card application? e.g. YYYY-MM-DD - 2017-07-26 "), '%Y-%m-%d')
today = datetime.today()
delta = then - today
print(abs(delta.days))
return abs(delta)
Here is the code that does NOT work when allowing a user to input the date in a MM-DD-YYYY input. I've tried casting both variables between int's and str's with no luck.
Error:
TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'str'
Code:
def get_days_since_last_app():
then = datetime.strptime(input("What is the date of your last credit "
"card application? e.g. MM-DD-YYYY - 07-26-2017 "),
'%m-%d-%Y')
today = arrow.now().format('MM-DD-YYYY')
delta = then - today
print(arrow.now().format('MM-DD-YYYY'))
print(delta)
print(abs(delta.days))
return abs(delta)