-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     
John
  • 321
  • 5
  • 16
  • 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. – figbeam Jul 28 '18 at 15:04
  • @figbeam: you may be right. I've re-opened the question. – Bryan Oakley Jul 28 '18 at 17:57

2 Answers2

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.

figbeam
  • 7,001
  • 2
  • 12
  • 18
  • 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('-','') – John Jul 29 '18 at 04:10
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).

Gabriel
  • 37
  • 3