Python 根据起始时间和结束时间计算时长
flyfish
%Y-%m-%d #年月日 大写Y年是4位 2021-01-01
%y-%m-%d #年月日 小写y年是两位 21-01-01
%H:%M:%S #时分秒
代码实现
import datetime
# begin = '08:01:34'
# end = '21:59:46'
begin = '2021-01-01'
end = '22-01-01'
# datebegin = datetime.datetime.strptime(begin,"%H:%M:%S")
# dateend = datetime.datetime.strptime(end,"%H:%M:%S")
datebegin = datetime.datetime.strptime(begin,"%Y-%m-%d")
dateend = datetime.datetime.strptime(end,"%y-%m-%d")
print(datebegin)
print(dateend)
diff = dateend - datebegin
print(diff.total_seconds()/60/60/24)
#print(diff.total_seconds()/60/60)