0

I have to ask the user to enter a start date and an end date. The date has to be in YYYY-MM-DD format. And I have already wrote some if statements that handle some formatting errors that the user might make, such as not inputting dashes in between, and also inputting the months and days in each other's spots.

What I need now is a way to make sure that the end date will always be later than the start date. Since logically an end date can't happen before the start date.

I don't know of a way to do this. Any help would be appreciated, thanks.

def get_start_date() -> str:
    START_DATE = input('Enter start date (in YYYY-MM-DD format): ').strip()

    if len(START_DATE) != 10:
        print('Date entry not correct, please try again')
        get_start_date()

    elif eval(START_DATE[5]) > 1:
        print('Date entry not correct, please try again')
        get_start_date()

    elif eval(START_DATE[8]) > 3:
        print('Date entry not correct, please try again')
        get_start_date()

    elif START_DATE[4] and START_DATE[7] != '-':
        print('Date entry not correct, please try again')
        get_start_date()



def get_end_date() -> str:
    END_DATE = input('Enter end date (in YYYY-MM-DD format): ').strip()

    if len(END_DATE) != 10:
        print('Date entry not correct, please try again')
        get_end_date()

    elif END_DATE[4] and END_DATE[7] != '-':
        print('Date entry not correct, please try again')
        get_end_date()

    elif eval(END_DATE[5]) > 1:
        print('Date entry not correct, please try again')
        get_end_date()



get_start_date()
get_end_date()
  • Try `import datetime` to start with. – rlms Feb 19 '14 at 20:12
  • What is this ` -> str` in your function definitions? – Ewan Feb 19 '14 at 20:15
  • Also, please don't use `eval`. It is really [dangerous](http://stackoverflow.com/questions/1832940/is-using-eval-in-python-a-bad-practice), and only very rarely necessarily. – rlms Feb 19 '14 at 20:31

1 Answers1

6

You could simplify this code; getting a start date is much the same as getting an end date, and the datetime module will do a lot of the work for you:

from datetime import datetime

def get_date(prompt, f="%Y-%m-%d"):
    while True:
        try:
            return datetime.strptime(input(prompt), f)
        except ValueError:
            print("Not a valid date.")

Now you are returning an actual datetime object, not just a string, and can easily compare:

start = get_date("Enter start date (YYYY-MM-DD): ")
while True:
    end = get_date("Enter end date (YYYY-MM-DD): ")
    if end > start:
        break
    print("End must be later than start.")
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • Also, let's say I had to get pieces of the object "start"or "end", say, after the dates are entered, I maybe just need the day, or the month to be available to me, could that me done? – Eric Cuevas Feb 19 '14 at 21:05
  • Or will it just be one big datetime object? – Eric Cuevas Feb 19 '14 at 21:06