>>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獲取某年中每個月的第一天和最后一天

系統 2120 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條評論
主站蜘蛛池模板: 绥芬河市| 冀州市| 邢台市| 资溪县| 彩票| 丰都县| 兴业县| 邻水| 岐山县| 蓬莱市| 凉城县| 嘉黎县| 开平市| 文安县| 鄯善县| 藁城市| 儋州市| 平遥县| 宣威市| 苏尼特左旗| 九寨沟县| 三河市| 龙川县| 万山特区| 屏南县| 衡阳市| 兰坪| 多伦县| 岳西县| 嘉祥县| 宁乡县| 崇义县| 韶山市| 明水县| 女性| 行唐县| 汕头市| 桐柏县| 房产| 伽师县| 柳州市|