Reputation: 27
I have written below python code for calculating number of days between two dates. The code runs fine , however it failed in one of the scenario, for example: if my start date is on 01/01/2016 and end date is on 28/02/2016 (dd/mm/yyyy format), then expected out put should be 58 days. However, my code gives result of 56 days. Below is the code snippet :
##Variable Declaration####################
daysofMonths = [31,28,31,30,31,30,31,31,30,31,30,31]
FinalCount = 0
####### Step 1: Leap Year Function####################################
######################################################################
##if (year is not divisible by 4) then (it is a common year)
##else if (year is not divisible by 100) then (it is a leap year)
##else if (year is not divisible by 400) then (it is a common year)
##else (it is a leap year)
######################################################################
def LeapYear(year):
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
return True
else:
return False
else:
return True
else:
return False
####### Step 2: Get the number of days for months Function####################################
def DaysforMonths(month,year):
x = 0
c = 0
for c in range(0,month,1):
if c == 1:
if LeapYear(year)== True:
x = x+29
else:
x = x+28
else:
x = x+ daysofMonths[c]
return x
####### Step 3: Get the inputs from user ###################################
year1 = int(input("Enter the year:"))
month1 = int(input("Enter the month:"))
day1 = int(input("Enter the day:"))
year2 = int(input("Enter the year:"))
month2 = int(input("Enter the month:"))
day2 = int(input("Enter the day:"))
####### Step 3.1: Get number of days between years function ###################################
def TotalDaysBetweenYear(year1,year2):
i = year1
countdays=0
if year1 == year2:
return countdays
else:
for i in range (year1,year2,1):
if (LeapYear(i)== True):
countdays = countdays+366
else:
countdays = countdays+365
return countdays
####### Step 4: Get the number of days between two dates Function####################################
def daysBetweenDates(year1, month1, day1, year2, month2, day2):
FinalCount = TotalDaysBetweenYear(year1,year2) - DaysforMonths(month1,year1)
FinalCount = FinalCount - day1
FinalCount = FinalCount + DaysforMonths(month2,year2)
FinalCount = FinalCount + day2
return FinalCount
print ("Number of days between two date is:",daysBetweenDates(year1, month1, day1, year2, month2, day2))
Upvotes: 0
Views: 6421
Reputation: 27
Below is the final code and it works for all the scenario. The intention was not to use datetime function, since I am learning Python and new to programming :)
##Variable Declaration####################
daysofMonths = [31,28,31,30,31,30,31,31,30,31,30,31]
FinalCount = 0
####### Step 1: Leap Year Function####################################
######################################################################
##if (year is not divisible by 4) then (it is a common year)
##else if (year is not divisible by 100) then (it is a leap year)
##else if (year is not divisible by 400) then (it is a common year)
##else (it is a leap year)
######################################################################
def LeapYear(year):
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
return True
else:
return False
else:
return True
else:
return False
####### Step 2: Get the number of days for months Function####################################
def DaysforMonths(month,year):
x = 0
c = 0
for c in range(0,month,1):
if c == 1:
if LeapYear(year)== True:
x = x+29
else:
x = x+28
else:
x = x+ daysofMonths[c]
return x
####### Step 3: Get the inputs from user ###################################
year1 = int(input("Enter the year:"))
month1 = int(input("Enter the month:"))
day1 = int(input("Enter the day:"))
year2 = int(input("Enter the year:"))
month2 = int(input("Enter the month:"))
day2 = int(input("Enter the day:"))
####### Step 3.1: Get number of days between years function ###################################
def TotalDaysBetweenYear(year1,year2):
i = year1
countdays=0
if year1 == year2:
return countdays
else:
for i in range (year1,year2,1):
if (LeapYear(i)== True):
countdays = countdays+366
else:
countdays = countdays+365
return countdays
####### Step 4: Get the number of days between two dates Function####################################
def daysBetweenDates(year1, month1, day1, year2, month2, day2):
if year1 == year2:
n = 0
j = 0
remDaysofEndMonth = 0
## Get the Total number of days between start month and last before end month
for j in range(month1-1,month2,1):
if j == 1:
if LeapYear(year1):
n = n+29
else:
n=n+28
else:
n=n+daysofMonths[j]
if month2-1 == 1:
if LeapYear(year1):
remDaysofEndMonth = 29 - (29-day2)
else:
remDaysofEndMonth = 28 - (28-day2)
else:
remDaysofEndMonth = daysofMonths[month2-1] - (daysofMonths[month2-1]-day2)
FinalCount = n + remDaysofEndMonth - day1
return FinalCount
else:
FinalCount = TotalDaysBetweenYear(year1,year2) - DaysforMonths(month1,year1)
FinalCount = FinalCount - day1
FinalCount = FinalCount + DaysforMonths(month2,year2)
FinalCount = FinalCount + day2
return FinalCount
print ("Number of days between two date is:",daysBetweenDates(year1, month1, day1, year2, month2, day2))
Upvotes: 1
Reputation: 1441
Your DaysforMonths
will calculate current month as it already over.
For example: DaysForMonth(01, 2016)
will return 31, but should return 0. Jenuary is not over yet.
You should not count current month.
Also (if possible) please follow pep8 when using python.
Upvotes: 0
Reputation: 727
Why on earth are you reimplementing the wheel and not just using datetime?
>>> import datetime
>>> start = datetime.date(2016,1,1)
>>> end = datetime.date(2016,2,28)
>>> end-start
datetime.timedelta(58)
>>> delta = end-start
>>> delta.days
58
Upvotes: 4