I am not sure if this is the appropriate title but I could not find a better one.
Say I have a date span:
12 Nov 2012 - 15 May 2014
Is there an easy way for separating this span to single year spans like:
12 Nov 2012 - 31 Dec 2012
01 Jan 2013 - 31 Dec 2013
01 Jan 2014 - 15 May 2014
You can easily turn a range expressed in datetime.date
objects into a sequence of (start, end)
objects within a year:
from datetime import date
def year_boundaries(start, end):
while start.year != end.year:
yield start, date(start.year, 12, 31)
start = date(start.year + 1, 1, 1)
yield (start, end)
Demo:
>>> for start, end in year_boundaries(date(2012, 11, 12), date(2014, 5, 15)):
... print start, '-', end
...
2012-11-12 - 2012-12-31
2013-01-01 - 2013-12-31
2014-01-01 - 2014-05-15
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.