Documentation

datetime.last

Return a datetime set to be the most recent preceeding specified weekday. For example, calling this function with day_of_week='monday' with a dt of a Friday, it will return the date of the Monday earlier that week. If called on Monday with day_of_week='monday', it will return the Monday of the previous week.

Times are stripped from the input datetime.

Arguments:

def datetime.last(
  dt: <datetime> = None,
  day_of_week: <str> = None, # 'monday', 'tuesday', ...
):

Returns <datetime>.

Example:


friday_in_january = datetime.from_date_and_time(
  year=2021,
  month=1,
  day=8,
  hour=0,
  minute=0,
  second=0)
  
# monday_in_january == Monday January 4 2021 00:00:00
monday_in_january = datetime.last(dt=friday_in_january,
  day_of_week='monday')