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

  • Home
  • SEARCH
  • 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 730407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:54:30+00:00 2026-05-14T06:54:30+00:00

we are trying to create a calendar function in python. we have created a

  • 0

we are trying to create a calendar function in python. we have created a small content management system, the requirement is, there will be a drop down list on the top right hand corner of the website, which will give the options – Months – 1 month, 2 months, 3 months and so on…, if the user selects 8 months then it should show the postscount for the last 8 months. the issue is we tried to write a small code which would do the month calculations, but the bug is that it does not consider the months beyond the current year, it shows the postscount only for months of the current year.

for example: if the user selects 3 months, it will show the count for the l 3 months i.e present month and the previous 2 months, but if the user selects option more than 4 months, it does not consider the months from previous year, it still shows the month of the present year only.

I am pasting the code below:-


def __getSpecifiedMailCount__(request, value):

    dbconnector= DBConnector()
    CdateList= "select cdate from mail_records"

    DateNow= datetime.datetime.today()
    DateNow= DateNow.strftime("%Y-%m")
    DateYear= datetime.datetime.today()
    DateYear= DateYear.strftime("%Y")
    DateMonth= datetime.datetime.today()
    DateMonth= DateMonth.strftime("%m")

    #print DateMonth


    def getMonth(value):
        valueDic= {"01": "Jan", "02": "Feb", "03": "Mar", "04": "Apr", "05": "May", "06": "Jun", "07": "Jul", "08": "Aug", "09": "Sep", "10": "Oct", "11": "Nov", "12": "Dec"}
        return valueDic[value]


    def getMonthYearandCount(yearmonth):
        MailCount= "select count(*) as mailcount from mail_records where cdate like '%s%s'" % (yearmonth, "%")
        MailCountResult= MailCount[0]['mailcount']
        return MailCountResult


    MailCountList= []    
    MCOUNT= getMonthYearandCount(DateNow)
    MONTH= getMonth(DateMonth)
    MailCountDict= {}
    MailCountDict['monthyear']= MONTH + ' ' + DateYear
    MailCountDict['mailcount']= MCOUNT
    var_monthyear= MONTH + ' ' + DateYear
    var_mailcount= MCOUNT
    MailCountList.append(MailCountDict)


    i=1
    k= int(value)
    hereMONTH= int(DateMonth)

    while (i < k):
        hereMONTH= int(hereMONTH) - 1
        if (hereMONTH < 10):
            hereMONTH = '0' + str(hereMONTH)
        if (hereMONTH == '00') or (hereMONTH == '0-1'):
            break
        else:
            PMONTH= getMonth(hereMONTH)
            hereDateNow= DateYear + '-' + PMONTH
            hereDateNowNum= DateYear + '-' + hereMONTH
            PMCOUNT= getMonthYearandCount(hereDateNowNum)
            MailCountDict= {}
            MailCountDict['monthyear']= PMONTH + ' ' + DateYear
            MailCountDict['mailcount']= PMCOUNT
            var_monthyear= PMONTH + ' ' + DateYear
            var_mailcount= PMCOUNT
            MailCountList.append(MailCountDict)
        i = i + 1


    #print MailCountList                

    MailCountDict= {'monthmailcount': MailCountList}    
    reportdata = MailCountDict['monthmailcount']
    #print reportdata
    return render_to_response('test.html', locals())

  • 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-14T06:54:30+00:00Added an answer on May 14, 2026 at 6:54 am

    You can use timedelta in the datetime module to subtract months.

    from datetime import datetime, timedelta
    
    now = datetime.now()
    four_months_ago = now - timedelta(days=(4*365)/12)
    

    This will keep track of moving back a year when necessary…

    >>> january_first = datetime(2009, 1,1)
    >>> january_first - timedelta(days=(4*365)/12)
    datetime.datetime(2008, 9, 2, 0, 0)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a calendar using the Gallery widget. I have a
I am trying to create a calendar instance. I have imported the java.util.calendar but
I am trying to create a PHP script that will create a calendar event
I'm trying to create calendar for android. There is a month switcher (previous/next month).
I am trying to create a calendar in android which will display the number
I am trying to create a drop down calendar. No jquery! Pure javascript. So
I am trying to create a virtual javascript calendar drop down. I want the
I'm trying to create simple script that subscribes a user to a company calendar.
I am trying to create a function that can convert a month number to
I am trying to create a calendar view for events. Before my trials this

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.