广告
返回顶部
首页 > 资讯 > 后端开发 > Python >利用python获取某年中每个月的第一天和最后一天
  • 469
分享到

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

天和每个月python 2022-06-04 18:06:30 469人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

搜索关键字: python get every first day of month 参考解答: 方法一: >>> import calendar >>> calen

搜索关键字:

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能有一定的帮助,如果有疑问大家可以留言交流。

--结束END--

本文标题: 利用python获取某年中每个月的第一天和最后一天

本文链接: https://www.lsjlt.com/news/13906.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作