datetime.nth_of
Return a datetime
set to be the nth interation of some unit of time given by unit
. Acceptable values for unit
are "month"
, "quarter"
, and "year"
.
Week days must be passed to this function. Setting the unit to 'month'
, the week day to 'monday
', and nth to 3
will return the 3rd Monday of the month, for example.
Times are stripped from the input datetime
.
Arguments:
def datetime.nth_of(
dt: <datetime> = None,
unit: <str> = None, # 'hour', 'day', 'week', 'month', or 'year'
day_of_week: <str> = None, # 'monday', 'tuesday', ...
nth: <dec> = None,
):
Returns <datetime>
.
Example:
long_long_ago = datetime.from_date_and_time(
year=1995,
month=1,
day=15,
hour=0,
minute=0,
second=0)
# third_monday_1995 == Monday January 16 1995 00:00:00
third_monday_1995 = datetime.nth_of(dt=long_long_ago, unit='year',
day_of_week='monday', nth=3)