Skip to content

Week counters #221

@pmdevita

Description

@pmdevita

It would be nice to be able to get a Date's week as counted from the start of the month or the start of the year, similar to Pendulum's .week_of_month and .week_of_year. https://pendulum.eustace.io/docs/#attributes-and-properties

If it helps, here's how I implemented these in one of my projects.

def get_week_of_month(date: Date) -> int:
    # Get the first day of the month
    first = date.replace(day=1)
    # Snap first back to the first Monday of that week
    first = first.subtract(days=first.day_of_week().value - 1)
    # How many full weeks have passed since then?
    week = (date.days_since(first) // 7) + 1
    return week


def get_week_of_year(date: Date) -> int:
    # Get the first day of the year
    first = date.replace(day=1, month=1)
    # Snap first back to the first Monday of that week
    first = first.subtract(days=first.day_of_week().value - 1)
    # How many full weeks have passed since then?
    week = (date.days_since(first) // 7) + 1
    return week

There might be other counters that would be useful but I couldn't think of any more off the top of my head. Now that I'm thinking about it as well, maybe a .weeks_since() method would make sense since there's already a .days_since() method? Or maybe a more arbitrary implementation that could work with many intervals.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions