Use python to obtain the first day and last day of each month in a year.
Search Keyword:
python get every first day of month
Reference:
Method 1:
>>> 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]
Method 2:
import datetimefor 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
Reference link:
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
Summary
The above is all about this article. I hope this article will help you learn or use python. If you have any questions, please leave a message.