python獲取指定日期范圍內(nèi)的每一天,每個(gè)月,每季度的方法
1.獲取所有天,返回一個(gè)列表:
def getBetweenDay(begin_date):
date_list = []
begin_date = datetime.datetime.strptime(begin_date, "%Y-%m-%d")
end_date = datetime.datetime.strptime(time.strftime('%Y-%m-%d',time.localtime(time.time())), "%Y-%m-%d")
while begin_date <= end_date:
date_str = begin_date.strftime("%Y-%m-%d")
date_list.append(date_str)
begin_date += datetime.timedelta(days=1)
return date_list
2.獲取所有月,返回一個(gè)列表:
def getBetweenMonth(begin_date):
date_list = []
begin_date = datetime.datetime.strptime(begin_date, "%Y-%m-%d")
end_date = datetime.datetime.strptime(time.strftime('%Y-%m-%d', time.localtime(time.time())), "%Y-%m-%d")
while begin_date <= end_date:
date_str = begin_date.strftime("%Y%m")
date_list.append(date_str)
begin_date = add_months(begin_date,1)
return date_list
def add_months(dt,months):
month = dt.month - 1 + months
year = dt.year + month / 12
month = month % 12 + 1
day = min(dt.day, calendar.monthrange(year, month)[1])
return dt.replace(year=year, month=month, day=day)
3.獲取所有季度,返回一個(gè)列表:
def getBetweenMonth(begin_date):
date_list = []
begin_date = datetime.datetime.strptime(begin_date, "%Y-%m-%d")
end_date = datetime.datetime.strptime(time.strftime('%Y-%m-%d', time.localtime(time.time())), "%Y-%m-%d")
while begin_date <= end_date:
date_str = begin_date.strftime("%Y-%m")
date_list.append(date_str)
begin_date = add_months(begin_date,1)
return date_list
def add_months(dt,months):
month = dt.month - 1 + months
year = dt.year + month / 12
month = month % 12 + 1
day = min(dt.day, calendar.monthrange(year, month)[1])
return dt.replace(year=year, month=month, day=day)
def getBetweenQuarter(begin_date):
quarter_list = []
month_list = getBetweenMonth(begin_date)
for value in month_list:
tempvalue = value.split("-")
if tempvalue[1] in ['01','02','03']:
quarter_list.append(tempvalue[0] + "Q1")
elif tempvalue[1] in ['04','05','06']:
quarter_list.append(tempvalue[0] + "Q2")
elif tempvalue[1] in ['07', '08', '09']:
quarter_list.append(tempvalue[0] + "Q3")
elif tempvalue[1] in ['10', '11', '12']:
quarter_list.append(tempvalue[0] + "Q4")
quarter_set = set(quarter_list)
quarter_list = list(quarter_set)
quarter_list.sort()
return quarter_list
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于np.meshgrid函數(shù)中的indexing參數(shù)問題
Meshgrid函數(shù)在二維與三維空間中用于生成坐標(biāo)網(wǎng)格,便于進(jìn)行圖像處理和空間數(shù)據(jù)分析,二維情況下,默認(rèn)使用笛卡爾坐標(biāo)系,而三維meshgrid則涉及不同的坐標(biāo)軸取法,在三維情況下,可能會(huì)出現(xiàn)坐標(biāo)軸排列序混亂2024-09-09
使用python+Pyqt5實(shí)現(xiàn)串口調(diào)試助手
這篇文章主要介紹了使用python+Pyqt5實(shí)現(xiàn)串口調(diào)試助手,串口通訊程序首先要對(duì)串口進(jìn)行設(shè)置,如波特率、數(shù)據(jù)位、停止位、校驗(yàn)位等,需要的朋友可以參考下2022-04-04
python GUI庫圖形界面開發(fā)之PyQt5多行文本框控件QTextEdit詳細(xì)使用方法實(shí)例
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5多行文本框控件QTextEdit詳細(xì)使用方法實(shí)例,需要的朋友可以參考下2020-02-02
在PyCharm中控制臺(tái)輸出日志分層級(jí)分顏色顯示的方法
今天小編就為大家分享一篇在PyCharm中控制臺(tái)輸出日志分層級(jí)分顏色顯示的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-07-07
Python中Sorted()函數(shù)的key參數(shù)使用方法詳解
這篇文章主要介紹了關(guān)于Python中Sorted()函數(shù)的key參數(shù)使用方法 ,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-06-06
Python實(shí)現(xiàn)簡(jiǎn)單2048小游戲
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)簡(jiǎn)單2048小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05

