python学习笔记(七)

求两具体日期间差的天数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
month=[0,31,28,31,30,31,30,31,31,30,31,30,31]
def isLeap(year):
if year%4==0 and year%100!=0 or year%400!=0:
return 1
return 0
day1=int(input())
day2=int(input())
y1=day1//10000
m1=day1//100%100
d1=day1%100
y2=day2//10000
m2=day2//100%100
d2=day2%100
ans=0
# if day1<day2:
# swap(day1,day2)
while y1<y2 or m1<m2 or d1<d2:
d1+=1
if isLeap(y1):
month[2]=29
else:
month[2]=28
if d1==month[m1]+1:
m1+=1
d1=1
if m1==13:
y1+=1
m1=1
ans+=1
print(ans)

具体日期的后n天

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
month=[0,31,28,31,30,31,30,31,31,30,31,30,31]
def isLeap(year):
if year%4==0 and year%100!=0 or year%400!=0:
return 1
return 0
day1=int(input("日期"))
sum=int(input("后几天"))
y1=day1//10000
m1=day1//100%100
d1=day1%100
while sum>0:
d1+=1
if isLeap(y1):
month[2]=29
else:
month[2]=28
if d1==month[m1]+1:
m1+=1
d1=1
if m1==13:
y1+=1
m1=1
sum-=1
print(y1,m1,d1)