我有两个日期/时间字符串:
start_date = 10/2/2010 8:00:00
end_date = 10/2/2010 8:59:00
我需要编写一个函数来计算事件是否在将来,过去或者现在是否正在发生 - 我已经阅读了相当多的文档但是发现它很难让它工作
我在Python中没有用太多时间进行计算,所以任何帮助都会非常感激!
非常感谢
答案 0 :(得分:16)
from datetime import datetime
start_date = "10/2/2010 8:00:00"
end_date = "10/2/2010 8:59:00"
# format of date/time strings; assuming dd/mm/yyyy
date_format = "%d/%m/%Y %H:%M:%S"
# create datetime objects from the strings
start = datetime.strptime(start_date, date_format)
end = datetime.strptime(end_date, date_format)
now = datetime.now()
if end < now:
# event in past
elif start > now:
# event in future
else:
# event occuring now