用于需要比较天数时,比如删除大于M天前的日志,只更新小于N天的数据等;

def compare_time(time1, time2):
d1 = datetime.datetime.strptime(time1, '%Y-%m-%d %H:%M:%S')
d2 = datetime.datetime.strptime(time2, '%Y-%m-%d %H:%M:%S')
delta = d1 - d2
print("days is "+ delta.days)
if delta.days >= 30:
    return True
else:
    return False
time1 = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
time2 = '2019-10-07 12:12:12'
compare_time(time1,time2)
Last modification:May 8th, 2020 at 08:15 am