如何用Python编写计算两个时间点之间月差的代码?
- 内容介绍
- 文章标签
- 相关推荐
本文共计93个文字,预计阅读时间需要1分钟。
pythondef monthdelta(d1, d2): delta=0 while True: mdays=monthrange(d1.year + d1.month, d1.year + d1.month + 1)[1] d1 +=timedelta(days=mdays) if d1==d2: delta +=1 else: break return delta
传入两个datetime对象,计算date的时间差:
def monthdelta(d1, d2):delta = 0
while True:
mdays = monthrange(d1.year, d1.month)[1]
d1 += timedelta(days=mdays)
if d1 <= d2:
delta += 1
else:
break
return delta
传入两个datetime.date的时间
本文共计93个文字,预计阅读时间需要1分钟。
pythondef monthdelta(d1, d2): delta=0 while True: mdays=monthrange(d1.year + d1.month, d1.year + d1.month + 1)[1] d1 +=timedelta(days=mdays) if d1==d2: delta +=1 else: break return delta
传入两个datetime对象,计算date的时间差:
def monthdelta(d1, d2):delta = 0
while True:
mdays = monthrange(d1.year, d1.month)[1]
d1 += timedelta(days=mdays)
if d1 <= d2:
delta += 1
else:
break
return delta
传入两个datetime.date的时间

