Skip to content

Add ordinal weekday support to calendar module (or datetime) #154847

Description

@bordenc

Feature or enhancement

Proposal:

The calendar or datetime modules should have a way of calculating the nth weekday of a month. The use case is that many laws define a statutory holidays as the nth weekday of a month (this includes most US holidays). This would complement the datetime.easter function and have use outside Christian-majority countries.

Guided by this Stack Overflow question, I wrote this function:

def ordinalday(year, month, nth, weekday) -> datetime.date:
    monthcal = calendar.Calendar(firstweekday=calendar.SUNDAY).monthdatescalendar(year, month)
    return [day for week in monthcal for day in week
            if day.weekday() == weekday and
            day.month == month][nth - 1]

This is probably not the Pythonic or robust way to do this, which strengthens my argument that the algorithm should be standardised.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

Honestly, I'm hesitant to make another website account to discuss one feature.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions