-1

Using a Tkinter input box, I ask a user for a date in the format YYYYMMDD. I would like to check if the date has been entered in the correct format , otherwise raise an error box. The following function checks for an integer but just need some help on the next step i.e the date format.

    def retrieve_inputBoxes():
        startdate = self.e1.get()  # gets the startdate value from input box
        enddate = self.e2.get()    # gets the enddate value from input box
        if startdate.isdigit() and enddate.isdigit():
           pass
        else:
            tkinter.messagebox.showerror('Error Message', 'Integer Please!')
            return     
2
  • I think @John wanted to know not how to implement a validate function but how to tell if the format is correct, in which case a regex would be the apparent solution. Google"regex date format YYYYMMDD". You might want to change format to YYYY-MM-DD as it looks way more used in real life. Commented Jul 28, 2018 at 15:04
  • @figbeam: you may be right. I've re-opened the question. Commented Jul 28, 2018 at 17:57

2 Answers 2

1

The easiest way would probably be to employ regex. However, YYYYMMDD is apparently an uncommon format and the regex I found was complicated. Here's an example of a regex for matching the format YYYY-MM-DD:

import re

text = input('Input a date (YYYY-MM-DD): ')
pattern = r'(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])'
match = re.search(pattern, text)
if match:
    print(match.group())
else:
    print('Wrong format')

This regex will work for the twentieth and twentyfirst centuries and will not care how many days are in each month, just that the maximum is 31.

Sign up to request clarification or add additional context in comments.

1 Comment

this is s great way to check the date format - i then removed the characters '-' from the date by using the replace command e.g startdate=text.replace('-','')
1

Probably you've already solved this, but if anyone is facing the same issue you can also convert the data retrieved from the entry widgets to datetime format using the strptime method, and using a try statement to catch exceptions, like:

from datetime import *

def retrieve_inputBoxes():
    try:
        startdate = datetime.strptime(self.e1.get(), '%Y-%m-%d')
        enddate = datetime.strptime(self.e2.get(), '%Y-%m-%d')
    except:
        print('Wrong datetime format, must be YYYY-MM-DD')
    else:
        print('startdate: {}, enddate: {}').format(startdate, enddate)

Note that the output string that will result will be something like YYYY-MM-DD HH:MM:SS.ssssss which you can truncate as follows the get only the date:

startdate = str(startdate)[0:10] #This truncates the string to the first 10 digits
enddate = str(enddate)[0:10] 

In my opinion, this method is better than the Regex method since this method also detects if the user tries to input an invalid value like 2019-04-31, or situations in which leap years are involved (i.e. 2019-02-29 = Invalid, 2020-02-29 = Valid).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.