forked from souravjain540/Basic-Python-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdays_between_dates.py
More file actions
55 lines (53 loc) · 1.45 KB
/
days_between_dates.py
File metadata and controls
55 lines (53 loc) · 1.45 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def leap_year(leap_days,year,start,step):
if year%100==0:
if year%400==0:
leap_days=start+1-step
else:
leap_days=start-step
elif year%4==0:
leap_days=start+1-step
else:
leap_days=start-step
return leap_days
def same_year(d1,d2):
days=0
if d1[1]==d2[1]:
days+=d2[0]-d1[0]
else:
days+=d2[0]
for i in range(d2[1]-1,d1[1],-1):
if i in months31:
days+=31
elif i in months30:
days+=30
elif i==2:
feb_days=0
days+=leap_year(feb_days,d1[2],28,0)
if d1[1] in months31:
days+=31-d1[0]
elif d1[1] in months30:
days+=30-d1[0]
elif d1[1]==2:
feb_days=0
days+=leap_year(feb_days,d1[2],28,d1[0])
return days
months31=[1,3,5,7,8,10,12]
months30=[4,6,9,11]
date1=input('Starting date (dd/mm/yyyy) - ')
date2=input('Ending date (dd/mm/yyyy) - ')
d1=date1.split('/')
d2=date2.split('/')
for i in range(3):
d1[i]=int(d1[i])
for i in range(3):
d2[i]=int(d2[i])
days=0
if d1[2]==d2[2]:
print(same_year(d1,d2))
else:
for j in range(d2[2],d1[2]+1,-1):
leap_year_days=0
days+=leap_year(leap_year_days,j,365,0)
days+=same_year([1,1,d2[2]],d2)
days+=same_year(d1,[31,12,d1[2]])+1
print(days)