Documentation

datetime.first_of

Return a datetime set to be the first of some unit of time given by unit. Acceptable values for unit are "month", "quarter", and "year".

Week days may also be passed to this function. Setting the unit to 'month' and the week day to 'monday' will return the first Monday of the month, for example.

Times are stripped from the input datetime.

Arguments:

def datetime.first_of(
  dt: <datetime> = None,
  unit: <str> = None, # 'quarter', 'month', or 'year'
  day_of_week: <str> = None, # 'monday', 'tuesday', ...
):

Returns <datetime>.

Example:

long_long_ago = datetime.from_date_and_time(
  year=1995,
  month=1,
  day=15,
  hour=0,
  minute=0,
  second=0)
  
# start_of_1995 == January 1 1995 00:00:00
start_of_1995 = datetime.first_of(dt=long_long_ago, unit='year')

# first_monday_1995 == Monday January 2 1995 00:00:00
first_monday_1995 = datetime.first_of(dt=long_long_ago, unit='month',
  day_of_week='monday')