搜索关键字:
参考解答:
方法一:
方法二:
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家学习或者使用
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
