2

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)
Caleb
  • 23
  • 3
  • "My attempts have returned a number of errors": Please include the _complete_ error message. – DYZ Feb 22 '18 at 07:25
  • You might have to do some checks on the input and do variations on how the inputted time is formated. And play around with `'%Y-%m-%d'` – Chuk Ultima Feb 22 '18 at 07:28
  • 1
    "My attempts have returned a number of errors" is not a precise enough error description for us to help you. *What* doesn't work? *How* doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ? – Jörg W Mittag Feb 22 '18 at 07:36
  • `My attempts have returned a number of errors` is something that a user would say, not something that a programmer would say. – jsotola Feb 22 '18 at 07:57
  • This is where I allow the user to input MM-DD-YYYY, but receive an error when trying to subtract arrow.now() from that inputted date. 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')) return abs(delta) – Caleb Feb 22 '18 at 18:03

1 Answers1

1
def get_days_since_last_app():
    then = pd.to_datetime(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 = pd.to_datetime(datetime.now().strftime('%Y-%m-%d'))
    delta = (then - today)
    print(delta.days)

I tried the write the code based on the question. Is this what you're looking for?

Jhonny
  • 71
  • 1
  • 9