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!
Is there a "standard" way to do this calculation in python???
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)
To add N months in a given date, create a relativedelta object representing the interval of N months and then add that to the datetime object to get the final date. Step 1: If the given date is in a string format, then we need to convert it to the datetime object. For that we can use the datetime. strptime() function.
To subtract N months from the current date, get the current date as datetime object. Then create a relativedelta object representing the interval of N months and then subtract that from the current date's datetime object to get the past date.
This addition can be performed by using datetime. timedelta() function. The timedelta() function is used for calculating differences in dates and also can be used for date manipulations in Python. It is one of the easiest ways to perform date manipulations.
To create a date, we can use the datetime() class (constructor) of the datetime module. The datetime() class requires three parameters to create a date: year, month, day.
If you're looking for exact or "more precise" dates, you're probably better off checking out dateutil.
Quick example:
>>> from dateutil.relativedelta import relativedelta >>> import datetime >>> TODAY = datetime.date.today() >>> TODAY datetime.date(2012, 3, 6)
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)
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With