Skip to content

Instantly share code, notes, and snippets.

@lppier
Last active December 26, 2018 03:39
Show Gist options
  • Save lppier/ee66169ce3dfb1a70a1e201f4316b8d3 to your computer and use it in GitHub Desktop.
Save lppier/ee66169ce3dfb1a70a1e201f4316b8d3 to your computer and use it in GitHub Desktop.
Get the last monday of the month of the date specified (Python)
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