利用python获取某年中每个月的第一天和最后一天
搜索关键字:
pythongeteveryfirstdayofmonth
参考解答:
方法一:
>>>importcalendar >>>calendar.monthrange(2002,1) (1,31) >>>calendar.monthrange(2008,2) (4,29) >>>calendar.monthrange(2100,2) (0,28) >>>calendar.monthrange(2016,2)[1]
方法二:
importdatetime
forxinxrange(1,13):
dt_start=(datetime.datetime(2016,x,1)).strftime("%Y%m%d")
if12==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")
printdt_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能有一定的帮助,如果有疑问大家可以留言交流。
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下