dioscuri
dioscuri

Reputation: 1

Python ValueError("time data %r does not match format %r" % even thought it is the correct format

I'm creating a database with GUI.To collect the date, I wanted to use tkcalendar. Yesterday, It worked perfectly fine but today, I changed some parts of the code without touching this part of the code.

from datetime import datetime
from tkinter import *
from tkinter import messagebox
from tkcalendar import DateEntry

root = Tk()

DOB = "Date of  Birth"
time_now = datetime.now()
DOBEntry = DateEntry(root, selectmode="day", textvariable=DOB)
DOBEntry.place(x=100, y=520)
def date_check():
    calendar_date = datetime.strptime(DOBEntry.get(), "%m%d%y")
    if calendar_date > time_now:
        messagebox.showerror("Invalid", "Selected date must not exceed current date")
        DOBEntry.set_date(time_now)
    root.after(100,date_check)
root.after(100,date_check)
print(DOBEntry)

root.mainloop()

The error states: ValueError: time data '11/21/22' does not match format '%m%d%y'

Since the format is correct and it worked yesterday, I expected no errors. I tried changing and shuffling the format to see if it on other formats. Sadly, no luck on my end

Upvotes: 0

Views: 907

Answers (1)

Thoughtful_monkey
Thoughtful_monkey

Reputation: 315

Sorry, this should be a comment, but I cannot comment yet. This worked on my end: datetime.datetime.strptime('11/21/22', '%m/%d/%y'). Given your date example.

Upvotes: 1

Related Questions