如果我有两个约会(例如。'8/18/2008'和'9/26/2008'),怎样才能得到这两个日期之间的天数?
当前回答
圣诞节前几天:
>>> import datetime
>>> today = datetime.date.today()
>>> someday = datetime.date(2008, 12, 25)
>>> diff = someday - today
>>> diff.days
86
这里有更多的算术。
其他回答
您需要datetime模块。
>>> from datetime import datetime
>>> datetime(2008,08,18) - datetime(2008,09,26)
datetime.timedelta(4)
另一个例子:
>>> import datetime
>>> today = datetime.date.today()
>>> print(today)
2008-09-01
>>> last_year = datetime.date(2007, 9, 1)
>>> print(today - last_year)
366 days, 0:00:00
正如这里所指出的
圣诞节前几天:
>>> import datetime
>>> today = datetime.date.today()
>>> someday = datetime.date(2008, 12, 25)
>>> diff = someday - today
>>> diff.days
86
这里有更多的算术。
不使用Lib只是纯代码:
#Calculate the Days between Two Date
daysOfMonths = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
def isLeapYear(year):
# Pseudo code for this algorithm is found at
# http://en.wikipedia.org/wiki/Leap_year#Algorithm
## if (year is not divisible by 4) then (it is a common Year)
#else if (year is not divisable by 100) then (ut us a leap year)
#else if (year is not disible by 400) then (it is a common year)
#else(it is aleap year)
return (year % 4 == 0 and year % 100 != 0) or year % 400 == 0
def Count_Days(year1, month1, day1):
if month1 ==2:
if isLeapYear(year1):
if day1 < daysOfMonths[month1-1]+1:
return year1, month1, day1+1
else:
if month1 ==12:
return year1+1,1,1
else:
return year1, month1 +1 , 1
else:
if day1 < daysOfMonths[month1-1]:
return year1, month1, day1+1
else:
if month1 ==12:
return year1+1,1,1
else:
return year1, month1 +1 , 1
else:
if day1 < daysOfMonths[month1-1]:
return year1, month1, day1+1
else:
if month1 ==12:
return year1+1,1,1
else:
return year1, month1 +1 , 1
def daysBetweenDates(y1, m1, d1, y2, m2, d2,end_day):
if y1 > y2:
m1,m2 = m2,m1
y1,y2 = y2,y1
d1,d2 = d2,d1
days=0
while(not(m1==m2 and y1==y2 and d1==d2)):
y1,m1,d1 = Count_Days(y1,m1,d1)
days+=1
if end_day:
days+=1
return days
# Test Case
def test():
test_cases = [((2012,1,1,2012,2,28,False), 58),
((2012,1,1,2012,3,1,False), 60),
((2011,6,30,2012,6,30,False), 366),
((2011,1,1,2012,8,8,False), 585 ),
((1994,5,15,2019,8,31,False), 9239),
((1999,3,24,2018,2,4,False), 6892),
((1999,6,24,2018,8,4,False),6981),
((1995,5,24,2018,12,15,False),8606),
((1994,8,24,2019,12,15,True),9245),
((2019,12,15,1994,8,24,True),9245),
((2019,5,15,1994,10,24,True),8970),
((1994,11,24,2019,8,15,True),9031)]
for (args, answer) in test_cases:
result = daysBetweenDates(*args)
if result != answer:
print "Test with data:", args, "failed"
else:
print "Test case passed!"
test()
如果您没有日期处理库(或者您怀疑它有bug),这里有一个抽象算法,应该可以很容易地翻译成大多数语言。
在每个日期执行以下计算,然后简单地减去两个结果。所有的商和余数都是正整数。
步骤a:首先确定日期的部分为Y(年),M(月)和D(日)。这些变量会随着我们的进行而改变。
步骤b: M减去3
(所以一月是-2,十二月是9)。
步骤c:如果M是负的,M加12,Y减1。
(这将“一年的开始”更改为3月1日,月份编号为0(3月)到11(2月)。这样做的原因是为了“一年之内的天数”在闰年和普通年之间不会改变,这样“短”的月份就在一年的年底,所以下一个月就不需要特殊处理了。)
步骤D。 M除以5得到商Q₁,余数R₁。在d上增加Q₁× 153,下一步使用R₁。
(从3月1日开始,每5个月有153天。)
步骤E.将R₁除以2得到商Q₂,忽略余数。在D上加R₁× 31 - Q₂。
(在每组5个月中,每2个月有61天,每对月的第一个月为31天。忽略2月比30天短的事实是安全的,因为在这一点上你只关心2月1日的天数,而不是第二年的3月1日。)
步骤D & E组合-替代法
在第一次使用之前,设置L=[0,31,61,92,122,153,184,214,245,275,306,337]
(这是在每个月的第一天之前的(调整后的)年的累计天数。)
在D后面加上L[M]。
步骤F 如果你使用儒略历日期而不是公历日期,请跳过这一步;这个日期在不同的国家有所不同,但在大多数英语国家是1752年9月3日,在大多数欧洲国家是1582年10月4日。
如果您确定永远不需要处理1-Mar-1900到28-Feb-2100范围之外的日期,您也可以跳过这一步,但随后必须对您处理的所有日期做出相同的选择。
Y₃除以100,得到Q₃,余数R₃。将Q₃除以4,得到另一个商式Q₄,忽略余数。在D中加入Q₄+ 36524 × Q₃。
将R₃₃Y。
一步G。 将Y除以4得到商Q₅,并忽略其余部分。在D上增加Q₅+ 365 × Y。
步骤H.(可选) 您可以在D中添加您选择的常数,以强制某个特定日期具有特定的日号。
对每个日期执行步骤A~G,得到D₁和D₂。
我一步。 D₁与D₂相减,得到D₂在D₁后的天数。
最后,一个评论:在处理1760年之前的日期时要格外谨慎,因为对哪一个月是一年的开始没有一致意见;许多地方把3月1日作为新年。
它也可以很容易地用箭头完成:
import arrow
a = arrow.get('2017-05-09')
b = arrow.get('2017-05-11')
delta = (b-a)
print delta.days
参考:http://arrow.readthedocs.io/en/latest/
推荐文章
- 如何在Flask-SQLAlchemy中按id删除记录
- 在Python中插入列表的第一个位置
- Python Pandas只合并某些列
- 如何在一行中连接两个集而不使用“|”
- 从字符串中移除前缀
- 代码结束时发出警报
- 如何在Python中按字母顺序排序字符串中的字母
- 我怎么能计算在打字稿2日期之间的时间
- 在matplotlib中将y轴标签添加到次要y轴
- 如何消除数独方块的凹凸缺陷?
- 为什么出现这个UnboundLocalError(闭包)?
- 使用Python请求的异步请求
- 将dd-mm-yyyy字符串转换为日期
- 如何检查一个对象是否是python中的生成器对象?
- 如何从Python包内读取(静态)文件?