python日历下拉框_python日历来计算月份倒退

我们正在尝试在python中创建日历函数.我们创建了一个小型的内容管理系统,要求是,网站的右上角会有一个下拉列表,它会给出选项 – 月 – 1个月,2个月,3个月等等. ..,如果用户选择8个月,那么它应该显示过去8个月的postscount.问题是我们试图编写一个可以进行月计算的小代码,但问题是它没有考虑当年之后的几个月,它只显示当年的几个月的postscount.

例如:如果用户选择3个月,它将显示l 3个月(即当前月份和前2个月)的计数,但如果用户选择超过4个月的选项,则不会考虑上一年的月份,它仍然只显示今年的月份.

我粘贴下面的代码: –

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

解决方法:

您可以在datetime模块中使用timedelta来减去月份.

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)

标签:python,calendar

来源: https://codeday.me/bug/20190607/1191078.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值