您如何添加“ 3个月”到python中的datetime.date对象? [英] How do you add "3 months" to a datetime.date object in python?

查看:119
本文介绍了您如何添加“ 3个月”到python中的datetime.date对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个python应用程序,需要每隔三个月绘制几年的日期。重要的是,日期每年精确地发生4次,并且日期应尽可能在每年的同一天出现,并且日期应尽可能在每月的同一天出现,并且日期应为尽可能接近 3个月(这是一个不断变化的目标,尤其是在leap年)。不幸的是, datetime.timedelta 不支持月份!

I have a python app that needs to plot out dates every three months for several years. It's important that the dates occur exactly 4 times a year, and that the dates occur on the same day each year as much as possible, and that the dates occur on the same day of the month as much as possible, and that the dates be as close to "3 months" apart as they can be (which is a moving target, especially on leap year). Unfortunately, datetime.timedelta doesn't support months!

是否存在标准方式进行此计算在python中?

Is there a "standard" way to do this calculation in python???

如果最糟的话,我会平底锅吃我的应用程序问PostgreSQL,它确实对日期计算有很好的内置支持,例如:

If worst comes to worst, I will punt and have my app ask PostgreSQL, who does have nice built-in support for date calculations, for the answer like this:

# select ('2010-11-29'::date + interval '3 months')::date;
    date    
------------
 2011-02-28
(1 row)


推荐答案

如果您要查找准确的日期或更精确的日期,最好检查一下< a href = http://labix.org/python-dateutil rel = noreferrer> dateutil 。

If you're looking for exact or "more precise" dates, you're probably better off checking out dateutil.

简单示例:

>>> from dateutil.relativedelta import relativedelta
>>> import datetime
>>> TODAY = datetime.date.today()
>>> TODAY
datetime.date(2012, 3, 6)

现在将今天,观察它与日期完全匹配(请注意, relativedelta(months = 3) relativedelta(month = 3)的行为不同。请确保在这些示例中使用 months !)。

Now add 3 months to TODAY, observe that it matches the day exactly (Note that relativedelta(months=3) and relativedelta(month=3) have different behaviors. Make sure to use months for these examples!).

>>> three_mon_rel = relativedelta(months=3)
>>> TODAY + three_mon_rel
datetime.date(2012, 6, 6)

并且保持一致在整个一年的过程中。从字面上看,每隔三个月一次(必须继续添加,因为出于某些原因,将 relativedelta 乘以并将其添加到 datetime.date 对象抛出 TypeError ):

And it stays consistent throughout the course of a year. Literally every three months, on the day (had to keep adding because for some reason multiplying a relativedelta and adding it to a datetime.date object throws a TypeError):

>>> TODAY + three_mon_rel + three_mon_rel
datetime.date(2012, 9, 6)
>>> TODAY + three_mon_rel + three_mon_rel + three_mon_rel
datetime.date(2012, 12, 6)
>>> TODAY + three_mon_rel + three_mon_rel + three_mon_rel + three_mon_rel
datetime.date(2013, 3, 6)

mVChr 的建议解决方案虽然绝对足够好,但会随着时间的推移而稍微漂移:

Whereas the mVChr's suggested solution, while definitely "good enough", drifts slightly over time:

>>> three_mon_timedelta = datetime.timedelta(days=3 * 365/12)
>>> TODAY + three_mon_timedelta
datetime.date(2012, 6, 5)

一年中的某天保持滑动:

And over the course of a year, the day of month keeps sliding:

>>> TODAY + three_mon_timedelta * 2
datetime.date(2012, 9, 4)
>>> TODAY + three_mon_timedelta * 3
datetime.date(2012, 12, 4)
>>> TODAY + three_mon_timedelta * 4
datetime.date(2013, 3, 5)

这篇关于您如何添加“ 3个月”到python中的datetime.date对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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