Skip to content

Instantly share code, notes, and snippets.

@5263
Created June 14, 2018 18:18
Show Gist options
  • Save 5263/774905606b262b67f6c4aed7a6b6eb94 to your computer and use it in GitHub Desktop.
Save 5263/774905606b262b67f6c4aed7a6b6eb94 to your computer and use it in GitHub Desktop.
Last day of previous months
import datetime,calendar
def ultimo(num=3,today=None):
"""return a list of datetime.date with the last day of num previous months"""
if today is None:
today = datetime.date.today()
return [datetime.date(y,m,calendar.monthrange(y,m)[1]) for \
m,y in (((today.month - i -1) % 12+1, today.year + \
((today.month - i -1) // 12)) for i in range(num+1,0,-1))]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment