-
Notifications
You must be signed in to change notification settings - Fork 3
/
Project16.py
83 lines (60 loc) · 1.75 KB
/
Project16.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Python3 program two find number of
# days between two given dates
# A date has day 'd', month
# 'm' and year 'y'
class Date:
def __init__(self, d, m, y):
self.d = d
self.m = m
self.y = y
# To store number of days in
# all months from January to Dec.
monthDays = [31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31]
# This function counts number of
# leap years before the given date
def countLeapYears(d):
years = d.y
# Check if the current year needs
# to be considered for the count
# of leap years or not
if (d.m <= 2):
years -= 1
# An year is a leap year if it is a
# multiple of 4, multiple of 400 and
# not a multiple of 100.
ans = int(years / 4)
ans -= int(years / 100)
ans += int(years / 400)
return ans
# This function returns number of
# days between two given dates
def getDifference(dt1, dt2):
# COUNT TOTAL NUMBER OF DAYS
# BEFORE FIRST DATE 'dt1'
# initialize count using years and day
n1 = dt1.y * 365 + dt1.d
# Add days for months in given date
for i in range(0, dt1.m - 1):
n1 += monthDays[i]
# Since every leap year is of 366 days,
# Add a day for every leap year
n1 += countLeapYears(dt1)
# SIMILARLY, COUNT TOTAL NUMBER
# OF DAYS BEFORE 'dt2'
n2 = dt2.y * 365 + dt2.d
for i in range(0, dt2.m - 1):
n2 += monthDays[i]
n2 += countLeapYears(dt2)
# return difference between
# two counts
return (n2 - n1)
# Driver Code
dob=input("Enter your DOB as DD/MM/YYYY : ")
dob=[int(i) for i in dob.split("/")]
dob=Date(dob[0],dob[1],dob[2])
PD=input("Enter Present Day as DD/MM/YYYY : ")
PD=[int(i) for i in PD.split("/")]
PD=Date(PD[0],PD[1],PD[2])
# Function call
print("\nThe number of days the person is alive:",getDifference(dob, PD))