python獲取指定日期范圍內(nèi)的每一天,每個月,每季度的方法
更新時間:2019年08月08日 10:28:11 作者:哈哈哈哈哈哈哈111
這篇文章主要介紹了python獲取指定日期范圍內(nèi)的每一天,每個月,每季度的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
1.獲取所有天,返回一個列表:
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.獲取所有月,返回一個列表:
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.獲取所有季度,返回一個列表:
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
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python實現(xiàn)圖片和視頻的相互轉(zhuǎn)換
有時候我們需要把很多的圖片合成視頻,或者說自己寫一個腳本去加快或者放慢視頻;也有時候需要把視頻裁剪成圖片,進行后續(xù)操作。這篇文章就將為大家介紹如何通過Python實現(xiàn)圖片和視頻的相互轉(zhuǎn)換,需要的可以參考一下2021-12-12Django的數(shù)據(jù)模型訪問多對多鍵值的方法
這篇文章主要介紹了Django的數(shù)據(jù)模型訪問多對多鍵值的方法,Django是Python豐富多彩的web框架中最具人氣的一個,需要的朋友可以參考下2015-07-07