2010-04-15 65 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