Python 两个日期之间的差异
出于某种原因,我真的被这个(相对论)问题难住了 如何计算两个日期之间的差异。我想在不使用模块的情况下执行此操作。但是由于某些原因,我的代码没有输出正确的答案 这是我的思考过程: 如果要求,计算2014年12月10日至2015年2月2日之间的天数Python 两个日期之间的差异,python,Python,出于某种原因,我真的被这个(相对论)问题难住了 如何计算两个日期之间的差异。我想在不使用模块的情况下执行此操作。但是由于某些原因,我的代码没有输出正确的答案 这是我的思考过程: 如果要求,计算2014年12月10日至2015年2月2日之间的天数 首先找出12月10日起(31-10)剩下的天数 =21天 查找12月和2月(又名1月)之间的月数,并添加 当月天数=31天 加上12月(21)剩下的天数+两个月(31)之间的天数+上个月(2)的天数=54天 然后检查异常情况,如闰年等 这是我的职责: d
def Calculate_Date (year1, month1, day1, year2, month2, day2):
"""
This function takes to dates (year/month/day) and returned the
difference between the dates
"""
#Create a dict for the # of days in each month
month_days = {1:31,2:28,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}
days_left_in_month1 = month_days[month1] - day1
days_left_in_year1 =0
days_into_year2 =0
days_between_year1_and_year2= 0
difference_in_days = 0
# Find the number days left in year one
i = month1
days_left_in_year = []
while i <= 12:
days = month_days[i]
days_left_in_year.append(days)
i = i + 1
days_left_in_year1 = (sum(days_left_in_year)) - day1
# Find the number days into year two
i = 1
days_into_year = []
while i <= month2:
days = month_days[i]
days_into_year.append(days)
i = i + 1
days_into_year2 = sum(days_into_year) - day2
#find the differernce in years
days_between_year1_and_year2 = (year2 - year1) * 365
#Check if its a leap year
leap_year = False
while True:
if float(year1 % 4) == 0:
if float(year1 % 100) != 0:
leap_year = True
break
if float(year1 % 100) == 0:
if float(year1 % 400) ==0:
leap_year = True
break
else:
break
#test output
print "The number of days left in the year One are %r " % days_left_in_year1
print "The number of days into the year Two are %r " % days_into_year2
print "The number of days between the years are %r " % days_between_year1_and_year2
#add an increment if leap year was true
if leap_year == True:
difference_in_days = days_left_in_year1 + days_into_year2 + days_between_year1_and_year2 + 1
else:
difference_in_days = days_left_in_year1 + days_into_year2 + days_between_year1_and_year2
return difference_in_days
print Calculate_Date(2011,6,30,2012,06,30)
def计算日期(第1年、第1个月、第1天、第2年、第2个月、第2天):
"""
此函数使用日期(年/月/日)并返回
日期之间的差异
"""
#为每个月的#天创建一个dict
月日={1:31,2:28,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}
月内剩余天数=月天数[月1]-第1天
剩余天数在年内1=0
进入第二年的天数=0
年1和年2之间的天数=0
以天为单位的差值=0
#查找第一年剩下的天数
i=月1
年内剩余天数=[]
虽然我没有做date2-date1
,但你可能会发现做(date2-x)-(date1-x)
更简单,其中x
是一个易于处理的日期,即year1
的“1月0日”
让我们定义两个函数:
def days_in_month(year, month):
"""
Return number of days in the specified month (28, 29, 30, or 31)
"""
if month == 2: # February
if not (year % 400):
return 29
elif not (year % 100):
return 28
elif not (year % 4):
return 29
else:
return 28
elif month in {1, 3, 5, 7, 8, 10, 12}:
return 31
else:
return 30
def days_in_year(year):
"""
Return the number of days in the specified year (365 or 366)
"""
return 337 + days_in_month(year, 2)
def days_this_year(year, month, day):
"""
Return the number of days so far this year
"""
return sum(days_in_month(year, m) for m in range(1, month)) + day
def year_days_since(base_year, this_year):
"""
Return the number of days from the start of base_year to the start of this_year
"""
if base_year > this_year:
raise ValueError("base_year must be <= this_year")
elif base_year == this_year:
return 0
else:
return sum(days_in_year(y) for y in range(base_year, this_year))
由于这个答案的对称性,它也会产生负面的差异:
date_diff(2001, 1, 3, 2002, 2, 5) # => 398 == 365 + 31 + 2
date_diff(2002, 2, 5, 2001, 1, 3) # => -398
与执行date2-date1
相比,您可能会发现执行(date2-x)-(date1-x)
更简单,其中x
是一个易于处理的日期,即year1
的“1月0日”
让我们定义两个函数:
def days_in_month(year, month):
"""
Return number of days in the specified month (28, 29, 30, or 31)
"""
if month == 2: # February
if not (year % 400):
return 29
elif not (year % 100):
return 28
elif not (year % 4):
return 29
else:
return 28
elif month in {1, 3, 5, 7, 8, 10, 12}:
return 31
else:
return 30
def days_in_year(year):
"""
Return the number of days in the specified year (365 or 366)
"""
return 337 + days_in_month(year, 2)
def days_this_year(year, month, day):
"""
Return the number of days so far this year
"""
return sum(days_in_month(year, m) for m in range(1, month)) + day
def year_days_since(base_year, this_year):
"""
Return the number of days from the start of base_year to the start of this_year
"""
if base_year > this_year:
raise ValueError("base_year must be <= this_year")
elif base_year == this_year:
return 0
else:
return sum(days_in_year(y) for y in range(base_year, this_year))
由于这个答案的对称性,它也会产生负面的差异:
date_diff(2001, 1, 3, 2002, 2, 5) # => 398 == 365 + 31 + 2
date_diff(2002, 2, 5, 2001, 1, 3) # => -398
如果这是真实代码,而不是学校作业,我会这样做:
from datetime import date
def date_diff(y1, m1, d1, y2, m2, d2):
return (date(y2, m2, d2) - date(y1, m1, d1)).days
如果这是真实代码,而不是学校作业,我会这样做:
from datetime import date
def date_diff(y1, m1, d1, y2, m2, d2):
return (date(y2, m2, d2) - date(y1, m1, d1)).days
你为什么不想使用标准模块来做这件事?我在一个在线编程课上得到了这个作业。在现实生活中,我知道我想使用datetime模块,但我也想知道为什么我没有正确使用它,所以请理解日期计算器的逻辑。请修复您问题中的缩进,代码不能按原样运行(只需粘贴代码,选择所有代码,然后点击工具栏中的{}
按钮).对不起,打印声明被删掉了。我现在已经添加了它。为什么你不想使用标准模块来做这件事呢?我在一个在线编程课上得到了这个作业。在现实生活中,我知道我想使用datetime模块,但我也想知道为什么我没有正确使用它,所以请理解日期计算器的逻辑。请修复您问题中的缩进,代码不能按原样运行(只需粘贴代码,选择所有代码,然后点击工具栏中的{}
按钮).对不起,打印声明被删掉了。我现在加上了。是的,在现实生活中我绝对会使用datetime。但我也想学习自己做这件事背后的逻辑。是的,在现实生活中我绝对会使用datetime。但我也想学习自己做这件事背后的逻辑。