Created
April 27, 2017 05:22
-
-
Save openrijal/82ba9870c63cde3f5575baa186520efb to your computer and use it in GitHub Desktop.
substract month from python datetime
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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