Skip to content

Instantly share code, notes, and snippets.

@openrijal
Created April 27, 2017 05:22
Show Gist options
  • Save openrijal/82ba9870c63cde3f5575baa186520efb to your computer and use it in GitHub Desktop.
Save openrijal/82ba9870c63cde3f5575baa186520efb to your computer and use it in GitHub Desktop.
substract month from python datetime
def monthdelta(date, delta):
m, y = (date.month+delta) % 12, date.year + ((date.month)+delta-1) // 12
if not m: m = 12
d = min(date.day, [31,
29 if y%4==0 and not y%400==0 else 28,31,30,31,30,31,31,30,31,30,31][m-1])
return date.replace(day=d,month=m, year=y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment