利用python获取某年中每个月的第一天和最后一天,python最后一天
利用python获取某年中每个月的第一天和最后一天,python最后一天
搜索关键字:
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能有一定的帮助,如果有疑问大家可以留言交流。
相关内容
- 浅谈Python浅拷贝、深拷贝及引用机制,浅谈python引用机
- 利用Python中SocketServer 实现客户端与服务器间非阻塞通信
- python中子类继承父类的__init__方法实例,python__init_
- python常见的格式化输出小结,python输出小结
- Python用UUID库生成唯一ID的方法示例,pythonuuid
- python用模块zlib压缩与解压字符串和文件的方法,
- python制作爬虫爬取京东商品评论教程,python爬虫
- 详解python开发环境搭建,详解python搭建
- 详解python中xlrd包的安装与处理Excel表格,pythonxlrd
- Python脚本获取操作系统版本信息,python脚本版本信息
评论关闭