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 981019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:28:30+00:00 2026-05-16T04:28:30+00:00

If only timedelta had a month argument in it’s constructor. So what’s the simplest

  • 0

If only timedelta had a month argument in it’s constructor. So what’s the simplest way to do this?

EDIT: I wasn’t thinking too hard about this as was pointed out below. Really what I wanted was any day in the last month because eventually I’m going to grab the year and month only. So given a datetime object, what’s the simplest way to return any datetime object that falls in the previous month?

  • 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-16T04:28:31+00:00Added an answer on May 16, 2026 at 4:28 am

    Try this:

    def monthdelta(date, delta):
        m, y = (date.month+delta) % 12, date.year + ((date.month)+delta-1) // 12
        if not m: m = 12
        d = min(date.day, [31,
            29 if y%4==0 and (not y%100==0 or y%400 == 0) else 28,
            31,30,31,30,31,31,30,31,30,31][m-1])
        return date.replace(day=d,month=m, year=y)
    
    >>> for m in range(-12, 12):
        print(monthdelta(datetime.now(), m))
    
        
    2009-08-06 16:12:27.823000
    2009-09-06 16:12:27.855000
    2009-10-06 16:12:27.870000
    2009-11-06 16:12:27.870000
    2009-12-06 16:12:27.870000
    2010-01-06 16:12:27.870000
    2010-02-06 16:12:27.870000
    2010-03-06 16:12:27.886000
    2010-04-06 16:12:27.886000
    2010-05-06 16:12:27.886000
    2010-06-06 16:12:27.886000
    2010-07-06 16:12:27.886000
    2010-08-06 16:12:27.901000
    2010-09-06 16:12:27.901000
    2010-10-06 16:12:27.901000
    2010-11-06 16:12:27.901000
    2010-12-06 16:12:27.901000
    2011-01-06 16:12:27.917000
    2011-02-06 16:12:27.917000
    2011-03-06 16:12:27.917000
    2011-04-06 16:12:27.917000
    2011-05-06 16:12:27.917000
    2011-06-06 16:12:27.933000
    2011-07-06 16:12:27.933000
    >>> monthdelta(datetime(2010,3,30), -1)
    datetime.datetime(2010, 2, 28, 0, 0)
    >>> monthdelta(datetime(2008,3,30), -1)
    datetime.datetime(2008, 2, 29, 0, 0)
    

    Edit Corrected to handle the day as well.

    Edit See also the answer from puzzlement which points out a simpler calculation for d:

    d = min(date.day, calendar.monthrange(y, m)[1])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using only ANSI C, what is the best way to, with fair certainty, determine
The only nice way I've found is: import sys import os try: os.kill(int(sys.argv[1]), 0)
Details: Only disable after user clicks the submit button, but before the posting back
The only thing I've found has been; .hang { text-indent: -3em; margin-left: 3em; }
The only experience I have so far with a touchscreen interface was one where
I only want a list of files that have been added (not ones that
I only just recently discovered that Visual C++ 2008 (and perhaps earlier versions as
Using only VBScript (launched from Windows Scripting Host) can I pull the DLL metadata
The only method provided by the DNN framework to get a module by ID
My only problem is making them line up three-across and have equal spacing. Apparently,

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.