python - 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()
解决方案
推荐阅读
- sql - 在 Pervasive SQL 中将数字转换为日期
- api - 如何通过 API 从 GooglePlay 和 Appstore 检索应用统计信息?
- css - CSS 从 Chrome 中的链接中删除朦胧的蓝色边框,更喜欢 Firefox 方法
- ios - Apple 开发者帐户 - 从代理中删除团队 ID
- java - JMeter4.0 - 采样器请求插件不采用更新的计数器值
- r - RStudio 中缺少构建工具
- node.js - 使用 IISNode 的带有 IIS 的 Node.js。我需要制定一个 DNS 规则,以便可以从服务器外部访问该站点,而无需修改主机文件
- c# - IAP Google Play 和 iTunes
- linux - 通过 wget 或 curl 获取 grafana 仪表板
- xslt-3.0 - 使用 xslt-3 合并两个或多个 xml 文件