pythontkintercalendar

Why is my tkcalendar taking the wrong year?


My tkcalendar Entry is setting 2000 for years before 2000, example if you answer your bithdate is 1986 it consider you being born in 2086.

birthDateEntry = DateEntry(root, width=12,background='darkblue', foreground='white', borderwidth=2,font = ("Copperplate Gothic Light", 10))

student = Student(fullNameEntrada.get(), birthDateEntry.get_date().strftime("%m/%d/%Y").replace("-", "/"))
        #For a 5/13/1989 it returns
        print(birthDateEntry.get()) #Return 5/13/2089
        print(birthDateEntry.get_date()) #Return 2089-05-13
#student class
class Student():
    
    def __init__(self, name, birthdate):
        self.name = name
        self.birthdate = birthdate
        self.age = self.getAge()
        self.registerDate=str(date.today().strftime("%m/%d/%Y").replace("-", "/"))      
        self.connection = sqlite3.connect('students.db')
        self.cursor = self.connection.cursor()
        self.createTable()


#getAge Method on student class
def getAge(self):
        today = date.today()
        birthyear = self.birthdate.split()
        self.age = today.year - int(birthyear[0][6:])
        return self.age```

Solution

  • I have done some further experimentation with this control. I notice that when the DateEntry control is first displayed it shows the date in the dropdown box in the format m/d/y, so today is shown as 5/13/24. If I then change that to 6/4/1989*, it ignores the century value and returns the date as 6/4/2089. if I now add the initial option date_pattern='dd/mm/yyyy' to the DateEntry constructor, it displays it in the form 13/05/2024. I can now change the date to 04/06/1989, and it returns it with the correct year. It seems that the display format of the year must be the full four digits for the control not to assume the current century.

    *in this case when cancelling the dropdown, or selecting a date from the expanded view, the display reverts to 6/4/89. So it would appear that the control parses that string to get the selected date.