August 2018
Intermediate to advanced
366 pages
10h 14m
English
Perform the following steps for this recipe:
import datetime
def shiftmonth(d, months):
for _ in range(abs(months)):
if months > 0:
d = d.replace(day=5) + datetime.timedelta(days=28)
else:
d = d.replace(day=1) - datetime.timedelta(days=1)
d = d.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
return d
>>> now = datetime.datetime.utcnow() >>> now datetime.datetime(2018, 3, 21, 21, 55, 5, 699400)
>>> shiftmonth(now, 1) datetime.datetime(2018, 4, 1, 0, 0)
>>> shiftmonth(now, ...