如何在python中将time_struct与datetime.datetime进行比较?

时间:2015-07-20 13:56:27

标签: python datetime

我试图摆脱超过7周的数据。我将str timestamp转换为time_struct,但是当我想与timedelta进行比较时,我得到了这个错误。

  

TypeError:unorderable类型:time.struct_time()< datetime.datetime()

这是我的代码

start = new_programme.get('start')
start_time = time.strptime(start, '%Y%m%d%H%M%S -0000')
print(type(start_time))
seven_weeks = datetime.datetime.now() - datetime.timedelta(weeks=7)
print(type(seven_weeks))
is_more_than_7_weeks = start_time < seven_weeks

stdout

<class 'time.struct_time'>
<class 'datetime.datetime'>

1 个答案:

答案 0 :(得分:3)

使用datetime.strptime:

start = new_programme.get('start')
start_time = datetime.datetime.strptime(start, '%Y%m%d%H%M%S -0000')
print(type(start_time))
seven_weeks = datetime.datetime.now() - datetime.timedelta(weeks=7)
print(type(seven_weeks))
is_more_than_7_weeks = start_time < seven_weeks