Does timedelta added to date consider leap year?

十年热恋 提交于 2021-02-08 11:59:30

问题


Example Suppose for a given date, when we add timedelta(days=180), and get the new date, does it consider the leap year and calculate the new date? Or do we exclusively calculate the leap year of the current date whether Feb has 28 / 29 days and get the new date accordingly in python datetime.datetime object?


回答1:


Try it out:

from datetime import datetime, timedelta

dt = datetime(2012, 2, 27)
print(dt + timedelta(3))  # March 1st

If it didn't handle February 29th, I would expect this to say March 2nd. So yes, Python's datetime knows about leap years.



来源:https://stackoverflow.com/questions/27477483/does-timedelta-added-to-date-consider-leap-year

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!