Last active
December 26, 2018 03:39
-
-
Save lppier/ee66169ce3dfb1a70a1e201f4316b8d3 to your computer and use it in GitHub Desktop.
Get the last monday of the month of the date specified (Python)
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
from datetime import date | |
from dateutil.relativedelta import relativedelta, MO | |
# Relative delta replaces the day in the date you specify by day = 31. From this new date, weekday=MO(-1) specifies the last monday. | |
def get_last_mon_of_mth(dt): | |
print (dt + relativedelta(day=31, weekday=MO(-1))) | |
get_last_mon_of_mth(date(2018, 12, 30)) | |
get_last_mon_of_mth(date(2019, 6, 4)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment