日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發布時間:2024/1/23 python 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

總結

以上是生活随笔為你收集整理的python日历下拉框_python日历来计算月份倒退的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。