def check_year(year):
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
return 1 # 整百年能被400整除的是闰年
else:
return 0
else:
return 1 # 非整百年能被4整除的为闰年
else:
return 0
def deal_time(time):
year=time.split('-')[0]
month=time.split('-')[1]
days=time.split('-')[2].split(' ')[0]
hours=time.split(' ')[1].split(':')[0]
return int(year),int(month),int(days),int(hours)
def day_hour_differ(time1,time2):
time1=str(time1)
time2=str(time2)
days=0
year,month1,days1,hours1=deal_time(time1)
year1,month2,days2,hours2=deal_time(time2)
if (hours2 - hours1)<0:
hours2=hours2+24
days2=days2-1
hour=hours2-hours1
else:
hour=hours2-hours1
if days2 - days1<0:
if month2 in [1,3,5,7,8,10,12]:
days2=days2+31
days=days2-days1
month2=month2-1
elif month2 in [4,6,9,11]:
days2=days2+30
days=days2-days1
month2=month2-1
elif check_year(year)==1:
days2=days2+29
days=days2-days1
month2=month2-1
elif check_year(year)==0:
days2=days2+28
days=days2-days1
month2=month2-1
else:
days=days2-days1
return days,hour
转载于:https://blog.51cto.com/linuxnewstar/1831572