This repository was archived by the owner on Nov 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathdays_you_lived.py
More file actions
36 lines (30 loc) · 1.3 KB
/
days_you_lived.py
File metadata and controls
36 lines (30 loc) · 1.3 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
# We assume that given dates are correct
# and
# solved for problem set in cs course on udacity.com
from calendar import isleap
def daysBetweenDates(year1, month1, day1, year2, month2, day2):
dom = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
domleap = [ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if (isleap(year1) and isleap(year2)):
e1 = sum(domleap) + sum(domleap[:month1 - 1]) + day1
e2 = sum(domleap) + sum(domleap[:month2 - 1]) + day2
return e2 - e1
days = 0
if isleap(year1):
days += (sum(domleap[month1 - 1:]) - day1) + sum(dom[:month2 - 1]) + day2
elif isleap(year2):
days += (sum(dom[month1 - 1:]) - day1) + sum(domleap[:month2 - 1]) + day2
else:
days += (sum(dom[month1 - 1:]) - day1) + sum(dom[:month2 - 1]) + day2
for year in range(year1 + 1, year2):
if isleap(year):
days += sum(domleap)
else:
days += sum(dom)
return days
date1=input("Enter a first date in YYYY-MM-DD format")
year1,month1,day1=map(int,date1.split('-'))
date2=input("Enter a second date in YYYY-MM-DD format")
year2,month2,day2=map(int,date2.split('-'))
days=daysBetweenDates(year1, month1, day1, year2, month2, day2)
print("Number of days between {} and {} is \n {}".format(date1,date2,abs(days)))