利用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能有一定的帮助,如果有疑问大家可以留言交流。
热门推荐
10 朋友新年祝福语大全 简短
11 新年祝福语简短大方兔年
12 搬新家礼物祝福语简短
13 同学见面花束祝福语简短
14 五一假期祝福语幽默简短
15 离职欢送敬酒祝福语简短
16 对学弟的祝福语简短
17 考老师辞职祝福语简短
18 祝福语驱散霉运的话简短