我們正試圖在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())
嘿,夥計,你解決了我的問題,感謝的人。我得到的東西工作。 – Suhail 2010-04-15 12:56:10
您可以點擊我答案左邊的複選按鈕,將其標記爲「已接受」答案 – 2010-04-15 15:48:25
如果您包括二月份,則此向後計數將失敗,因爲您將計算一月份的二月份。 通常這不會是一個問題,但你不希望它在今天= 3月1日或2日失敗,對不對? 有沒有簡單的方法來找出「n個月前」是哪一個月? – 2010-04-20 21:31:20