datetime_truncate_5.py 382 B

12345678910
  1. def truncate_week(datetime):
  2. '''
  3. Truncates a date to the first day of an ISO 8601 week, i.e. monday.
  4. :params datetime: an initialized datetime object
  5. :return: `datetime` with the original day set to monday
  6. :rtype: :py:mod:`datetime` datetime object
  7. '''
  8. datetime = truncate(datetime, 'day')
  9. return datetime - timedelta(days=datetime.isoweekday() - 1)