2月28日和2月29日相对增量
我有以下代码:
import datetime
from dateutil.relativedelta import relativedelta
date_1=datetime.date(2000,2,28)+relativedelta(years=+1)
date_2=datetime.date(2000,2,29)+relativedelta(years=+1)
print(date_1)
print(date_2)
date_1=date_1+relativedelta(years=+1)
date_2=date_2+relativedelta(years=+1)
print(date_1)
print(date_2)
date_1=date_1+relativedelta(years=+1)
date_2=date_2+relativedelta(years=+1)
print(date_1)
print(date_2)
这是输出:
2001-02-28
2001-02-28
2002-02-28
2002-02-28
2003-02-28
2003-02-28
我的问题是为什么 2 月 28 日和 29 日的输出相同?我想当我输入 29 时,最终的答案将是 1/3/2003 和 28最后的答案是28/2/2003。(不同的答案)。另外,当我这样做时:
import datetime
from dateutil.relativedelta import relativedelta
print(relativedelta(datetime.date(2003,2,28),datetime.date(2000,2,29)).years)
print(relativedelta(datetime.date(2003,2,28),datetime.date(2000,2,28)).years)
我得到:
3
3
我不知道为什么它是一样的。
I have the following code:
import datetime
from dateutil.relativedelta import relativedelta
date_1=datetime.date(2000,2,28)+relativedelta(years=+1)
date_2=datetime.date(2000,2,29)+relativedelta(years=+1)
print(date_1)
print(date_2)
date_1=date_1+relativedelta(years=+1)
date_2=date_2+relativedelta(years=+1)
print(date_1)
print(date_2)
date_1=date_1+relativedelta(years=+1)
date_2=date_2+relativedelta(years=+1)
print(date_1)
print(date_2)
And this is the output:
2001-02-28
2001-02-28
2002-02-28
2002-02-28
2003-02-28
2003-02-28
My question is why the output is the same for both 28 and 29 february?I thought when I enter 29 the answer in the end is gonna be 1/3/2003 and for 28 the answer in the end is gonna be 28/2/2003.(different answers).Additionally,when I'm doing:
import datetime
from dateutil.relativedelta import relativedelta
print(relativedelta(datetime.date(2003,2,28),datetime.date(2000,2,29)).years)
print(relativedelta(datetime.date(2003,2,28),datetime.date(2000,2,28)).years)
I get:
3
3
I don't know why it's the same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论