Skip to content

Date.nth_weekday(...) is a poor fit as a replacement for Date.start_of("week") #331

@tlauli

Description

@tlauli

Hi, the documentation mentions to use Date.nth_weekday(...) as a replacement for Date.start_of("week") but this does not really solve the problem nicely.
Assume that weeks start on Monday, and we have a d = Date(2026, 4, 20) (a Monday). Then there is no way in which I can call (d.nth_weekday(...), which would return d itself (I will either get the next Monday, or the previous one). Compare to Date(2026, 4, 1).start_of("month") == Date(2026, 4, 1).

This is mainly a request for consideration of an improvement of the api, as the problem can be worked around simply by d.subtract(days=d.day_of_week().value - 1).

My naive idea would be a method similar to the nth_weekday(n, weekday), that for n > 0 would work as it does now, but would allow n = 0 as a viable input, and would satisfy d.nth_weekday(n, weekday).subtract(weeks=m) == d.nth_weekday(n - m, weekday).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions