我正在尝试在我的 Flask 应用程序中格式化生成的日期datetime.datetime.utcnow()
(使用它,因为它相当于ISODate()
在 MongoDB 中,这是我的应用程序使用的数据库),但 Jinja2 不会呈现它们。
我的应用程序中有以下功能:
def format_isodate(timestamp):
"""Format a ISODate time stamp for display."""
date = datetime.datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S.%f")
return date.strftime('%Y-%m-%d @ %H:%M:%S')
日期如下所示:
"2013-07-04 20:06:05.088000"
我有以下过滤器:
app.jinja_env.filters['isodateformat'] = format_isodate
但是当我尝试在模板中格式化时间戳时:
{{ change.submit_date|isodateformat }}
我收到此错误:
“TypeError:必须是字符串,而不是 datetime.datetime”
我不明白为什么我会收到这个错误。不strftime()
将日期转换为字符串?