首页 > 解决方案 > Python中时间的格式检查

问题描述

我目前正在从事一个学校项目,我必须开发一个数据库系统,使最终用户能够为客户创建约会。我已经成功地为约会日期创建了格式检查,并且它运行良好,但是,我当时的格式检查返回 False,而它应该返回 True。任何人都可以在下面的代码中看到问题所在吗?先感谢您。

    def dateformat(self): #Function to perform a format check on the date of birth
        date = self.Entry3.get()
        try:
            if date != datetime.strptime(date, "%d/%m/%Y").strftime('%d/%m/%Y'):
                raise ValueError
            return True
        except ValueError:
            tkinter.messagebox.showerror("Error","Please enter the appointment date in the correct format DD/MM/YYYY")
            self.ClearDate()
            return False

    def timeformat(self):
        apptime = self.Entry4.get()
        try:
            if apptime != time.strptime(apptime, '%H:%M'):
                raise ValueError
            return True
        except ValueError:
            tkinter.messagebox.showerror("Error","Please enter the appointment time in the correct 24hr format HH:MM")
            self.ClearTime()
            return False

    def AddAppointment(self):
        appointmentid = self.Entry1.get()
        customerid = self.Entry2.get()
        date = self.Entry3.get()
        time = self.Entry4.get()
        staffid = self.Entry5.get()

        if self.dateformat() and self.timeformat():
            try:
                with sqlite3.connect("LeeOpt.db") as db:
                    cursor = db.cursor()
                    add_appointment = ('''INSERT INTO Appointments(AppointmentID, CustomerID, AppointmentDate, AppointmentTime, StaffID)
                    VALUES (?,?,?,?,?)''')
                    cursor.execute(add_appointment, [(appointmentid),(customerid),(date),(time),(staffid)])
                    tkinter.messagebox.showinfo("Notification","Appointment added successfully")
                    self.ClearEntries()
            except (sqlite3.IntegrityError):
                tkinter.messagebox.showerror("Error","This AppointmentID is already taken, please try another.")
                self.ClearEntries()

在此处输入图像描述

在此处输入图像描述

标签: pythondatabasesqlitevalidation

解决方案


推荐阅读