我写了下面的python代码来计算两个日期之间的天数。代码运行正常,但在其中一个场景中失败,例如:如果我的开始日期是2016年1月1日,结束日期是28/02/2016(年/月/日元格式),那么预计会出现放了应该是58天。但是,我的代码给出了56天的结果。 以下是代码段:
##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))
答案 0 :(得分:3)
为什么你重新实现轮子而不只是使用日期时间?
>>> 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
答案 1 :(得分:1)
以下是最终代码,适用于所有场景。目的不是使用datetime函数,因为我正在学习Python并且是编程的新手:)
##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))
答案 2 :(得分:0)
您的DaysforMonths
将计算已经结束的当月。
例如:DaysForMonth(01, 2016)
将返回31,但应返回0. Jenuary尚未结束。
你不应该算上当月。
另外(如果可能)请在使用python时遵循pep8。