python - 计算日期差
import time
import datetime
def CalTime(date1, date2):
# %Y-%m-%d %H:%M:%S为日期格式
# date1 = time.strptime(date1, '%Y-%m-%d %H:%M:%S')
# date2 = time.strptime(date2, '%Y-%m-%d %H:%M:%S')
# %Y-%m-%d为日期格式
date1 = time.strptime(date1, '%Y-%m-%d')
date2 = time.strptime(date2, '%Y-%m-%d')
date1 = datetime.datetime(date1[0], date1[1], date1[2])
date2 = datetime.datetime(date2[0], date2[1], date2[2])
return date2 - date1
CalTime('2023-02-01','2023-05-01').days
CalTime('2023-02-01','2023-05-01').seconds