Python date calculations, where art thou?
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???
The SQL way?
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)
If you’re looking for exact or “more precise” dates, you’re probably better off checking out dateutil.
Quick example:
Now add 3 months to
TODAY, observe that it matches the day exactly (Note thatrelativedelta(months=3)andrelativedelta(month=3)have different behaviors. Make sure to usemonthsfor these examples!).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
relativedeltaand adding it to adatetime.dateobject throws aTypeError):Whereas the mVChr‘s suggested solution, while definitely “good enough”, drifts slightly over time:
And over the course of a year, the day of month keeps sliding: