Flask TypeError:无法解决的类型:datetime.datetime()> STR()

时间:2015-12-13 10:02:00

标签: python datetime

我一直收到这个错误。我正在尝试将列表中每个项目的值设置为 dt ,以便从创建时间开始返回。

我不确定这是否是正确的方法。

有任何帮助吗?

with open("list.txt", "r", newline="") as tasksFile:
        tasksFileReader= csv.reader(tasksFile)
        dtList = []
        for row in tasksFileReader:
            if len (row[1]) !=0:
                dtList = dtList + [row[1]]
item=dtList[::1]
for item in dtList:
    dt=item
now = datetime.datetime.now()

if now > dt:
    diff = now - dt
    dt_is_past = True
else:
    diff = dt - now
    dt_is_past = False

periods = (
    (diff.days / 365, "year", "years"),
    (diff.days / 30, "month", "months"),
    (diff.days / 7, "week", "weeks"),
    (diff.days, "day", "days"),
    (diff.seconds / 3600, "hour", "hours"),
    (diff.seconds / 60, "minute", "minutes"),
    (diff.seconds, "second", "seconds"),
)


for period, singular, plural in periods:

    if period:
        time =  "%d %s %s" % (period, \
            singular if period == 1 else plural, \
            past_ if dt_is_past else future_)
    else:
        time = default 
return render_template('welcome.html', taskList = taskList, time=time,dtList=dtList)

1 个答案:

答案 0 :(得分:0)

您以字符串形式阅读日期并将其放入dt。然后尝试将其与日期时间对象进行比较,该对象失败。

相反,您希望使用reverse proxy解析dt,例如:

dt = datetime.strptime(dt, "%Y-%m-%d %H:%M:%S")

实际格式(第二个参数)取决于您尝试解析的日期格式,请参阅strptime()以获取参数列表。