2010-04-15 66 views
4

我們正試圖在python中創建一個日曆函數。我們已經創建了一個小型的內容管理系統,要求是,網站右上角會有一個下拉列表,它會給出選項 - 月份 - 1個月,2個月,3個月等等。 ..,如果用戶選擇8個月,那麼它應該顯示過去8個月的postscount。問題是我們試圖編寫一個可以進行月份計算的小代碼,但問題是它不考慮當年之後的月份,它僅顯示當月的月份後月份。例如:如果用戶選擇3個月,它將顯示l 3個月即當前月份和前2個月的計數,但是如果用戶選擇超過4個月的選項,則不考慮月份從前一年起,它仍然只顯示當年的月份。python日曆向後計算月份

我粘貼下面的代碼: -


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()) 

回答

-2

您可以使用timedeltadatetime模塊中減去個月。

from datetime import datetime, timedelta 

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

這將跟蹤一年在必要的時候搬回來的...

>>> january_first = datetime(2009, 1,1) 
>>> january_first - timedelta(days=(4*365)/12) 
datetime.datetime(2008, 9, 2, 0, 0) 
+0

嘿,夥計,你解決了我的問題,感謝的人。我得到的東西工作。 – Suhail 2010-04-15 12:56:10

+0

您可以點擊我答案左邊的複選按鈕,將其標記爲「已接受」答案 – 2010-04-15 15:48:25

+0

如果您包括二月份,則此向後計數將失敗,因爲您將計算一月份的二月份。 通常這不會是一個問題,但你不希望它在今天= 3月1日或2日失敗,對不對? 有沒有簡單的方法來找出「n個月前」是哪一個月? – 2010-04-20 21:31:20

1

這是一個尷尬的問題,因爲月份有不同的長度。什麼是7月31日減去一個月?我有類似的要求,但我做了一個簡單的假設,我一直想在本月的第一天,即n個月前。如果它是你的要求有幫助的,那就是:

from datetime import date 

def add_months(start_date, months): 
    return date(
     start_date.year + (start_date.month - 1 + months)/12, 
     (start_date.month - 1 + months) % 12 + 1, 
     1) 
6

在Python-dateutil包的relativedelta功能的伎倆:

from dateutil.relativedelta import * 
import datetime 

five_months_ago = datetime.datetime.now() - relativedelta(months=5) 
+0

這需要額外的軟件包:[python-dateutil](http://labix.org/python-dateutil)。 – orchistro 2012-12-31 07:36:42