-
Notifications
You must be signed in to change notification settings - Fork 29
Open
Labels
something newNew feature or requestNew feature or request
Description
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.
genevieve-me
Metadata
Metadata
Assignees
Labels
something newNew feature or requestNew feature or request