源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2021-11-04 12:28 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:利用python获取某年中每个月的第一天和最后一天
[b]搜索关键字:[/b] [code]python get every first day of month[/code] [b]参考解答:[/b] [b]方法一:[/b]
>>> 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]
[b]方法二:[/b]
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
[b]参考链接:[/b] 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 [b]总结[/b] 以上就是这篇文章的全部内容了,希望本文的内容对大家学习或者使用python能有一定的帮助,如果有疑问大家可以留言交流。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部