判断日期先后

from datetime import datetime

target_time = '2020-02-28 11:38:53'
format_pattern = '%Y-%m-%d %H:%M:%S'
cur_time = datetime.now()
# 将 'cur_time' 类型时间通过格式化模式转换为 'str' 时间
cur_time = cur_time.strftime(format_pattern)
difference = (datetime.strptime(target_time, format_pattern) - datetime.strptime(cur_time, format_pattern))
if difference.days < 0:
print(target_time, '在当前时间之前')
else:
print(target_time, '在当前时间之后')

posted @ 2021-06-22 16:23  布都御魂  阅读(97)  评论(0编辑  收藏  举报