利用python獲取某年中每個月的第一天和最后一天
搜索關(guān)鍵字:
python get every first day of month
參考解答:
方法一:
>>> import calendar >>> calendar.monthrange(2002,1) (1, 31) >>> calendar.monthrange(2008,2) (4, 29) >>> calendar.monthrange(2100,2) (0, 28) >>> calendar.monthrange(2016, 2)[1]
方法二:
import datetime for x in xrange(1, 13): dt_start = (datetime.datetime(2016, x, 1)).strftime("%Y%m%d") if 12 == x: dt_end = (datetime.datetime(2016, 12, 31)).strftime("%Y%m%d") else: dt_end = (datetime.datetime(2016, x+1, 1) - datetime.timedelta(days = 1)).strftime("%Y%m%d") print dt_start, dt_end
參考鏈接:
http://stackoverflow.com/questions/42950/get-last-day-of-the-month-in-python
https://docs.python.org/2/library/calendar.html
https://docs.python.org/2/library/datetime.html
http://stackoverflow.com/questions/22696662/python-list-of-first-day-of-month-for-given-period
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家學習或者使用python能有一定的幫助,如果有疑問大家可以留言交流。
相關(guān)文章
Python中的__new__與__init__魔術(shù)方法理解筆記
這篇文章主要介紹了Python中的__new__與__init__魔術(shù)方法理解筆記,需要的朋友可以參考下2014-11-11Python將Word文檔轉(zhuǎn)為PDF的兩種方法
這篇文章主要介紹了兩種將docx和doc文件轉(zhuǎn)換為PDF的方法,方法一使用了docx2pdf模塊,方法二使用了win32com模塊,文中通過代碼及圖文介紹的非常詳細,需要的朋友可以參考下2024-12-12Python實現(xiàn)過濾單個Android程序日志腳本分享
這篇文章主要介紹了Python實現(xiàn)過濾單個Android程序日志腳本分享,本文講解了原理、實現(xiàn)代碼、使用方法、最新代碼等內(nèi)容,需要的朋友可以參考下2015-01-01Pandas DataFrame 取一行數(shù)據(jù)會得到Series的方法
今天小編就為大家分享一篇Pandas DataFrame 取一行數(shù)據(jù)會得到Series的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11