Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7600793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:59:58+00:00 2026-05-30T22:59:58+00:00

Python date calculations, where art thou? I have a python app that needs to

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T22:59:59+00:00Added an answer on May 30, 2026 at 10:59 pm

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to calculate Hebrew dates (primarily the current Hebrew date) in Python. Which
Python and Matlab quite often have integer date representations as follows: 733828.0 733829.0 733832.0
Possible Duplicate: How can I subtract a day from a python date? I have
How do I do a check in python that a date appears in the
I've got the following python code that does a date comparison for a list
Possible Duplicate: Converting unix timestamp string to readable date in Python I have a
Using python I have to retrieve value of time and date of an event
I have python dict object with key as datetime.date object and values as tuple
In Python, I'm attempting to retrieve the date/time that is exactly 30 days (30*24hrs)
Using Python I would like to find the date object for last Wednesday. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.