>>importcalendar>>>calendar.monthrange(2002,1)(1,31)>>>calendar.monthrange(2008,2)(4,29)>>>calendar.monthrange(2100,2)(0,28)>>>calendar.monthrange(2016,2)[1]方法二:importdatetimeforxinxran" />

日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

利用python獲取某年中每個月的第一天和最后一天

系統 2139 0

搜索關鍵字:

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

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家學習或者使用python能有一定的幫助,如果有疑問大家可以留言交流。


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 嘉义县| 资阳市| 磐安县| 陵川县| 宁明县| 湟源县| 彰化市| 沁阳市| 临清市| 察哈| 家居| 土默特左旗| 克拉玛依市| 东平县| 香河县| 图木舒克市| 武宣县| 安化县| 和龙市| 阳山县| 兴城市| 五家渠市| 五台县| 聊城市| 象州县| 龙岩市| 科技| 广灵县| 溆浦县| 颍上县| 延津县| 重庆市| 南宁市| 肥乡县| 凭祥市| 河北区| 泽库县| 泰和县| 辛集市| 临洮县| 沙洋县|