到目前为止添加的timedelta是否考虑leap年? [英] Does timedelta added to date consider leap year?

查看:40
本文介绍了到目前为止添加的timedelta是否考虑leap年?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例假设对于给定日期,当我们添加timedelta(days = 180)并获得新日期时,是否考虑consider年并计算新日期?还是我们只计算当前日期的the年,即Feb是否具有28/29天,并在python datetime.datetime对象中相应地获取新日期?

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?

推荐答案

尝试一下:

from datetime import datetime, timedelta

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

如果它不能处理2月29日,我希望这可以说3月2日.所以是的,Python的日期时间知道know年.

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

这篇关于到目前为止添加的timedelta是否考虑leap年?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆