时间数据“(datetime.date(2021, 7, 30), )”与格式“%Y/%m/%d”不匹配

time data '(datetime.date(2021, 7, 30), )' does not match format '%Y/%m/%d'

我在我的 jupyterLab 笔记本中使用以下查询从数据库访问日期:

newDate = con.cursor()
newDate.execute("select max(CalendarDate) as cdates from db.table1 where cdates < getdate()")
c_date = newDate.fetchone()
cDate = str(c_date)
current_date = datetime.strptime(cDate,'%Y/%m/%d').strftime('%m/%d/%Y')
print(currentdate)

它给出了这个 ValueError: 时间数据“(datetime.date(2021, 7, 30), )”与格式“%Y/%m/%d”不匹配

谁能指导一下,正确的方法吗?

似乎 c_date 已经是一个 datetime.date 对象。你不需要 cDate = str(c_date).

尝试:

newDate = con.cursor()
newDate.execute("select max(CalendarDate) as cdates from db.table1 where cdates < getdate()")
c_date = newDate.fetchval()
current_date = c_date.strftime('%m/%d/%Y')
print(currentdate)