Suggest a new function method that returns the weeks range in a month

Currently in python, when you use the calendar module, you can get the days of the week, number of days in month. But I think it misses a function that can get all the different weeks in a month based of from the starting date. For example. the month of October has five(5) weeks. If I wanted to get the ranges of this week based on the starting day of the week and the ending day In the first week, I will have [[‘7-13’, ‘14-20’, ‘21-27’, ‘28-November-3’]. I suggest a python function that will return a list of the different month’s weeks. Here is
As shown in the screenshot below

image

Every user needs to write code to treat the last week however they wish but:

>>> import calendar
>>> calendar.setfirstweekday(calendar.MONDAY)
>>> [f'{week[0]}-{week[-1]}' for week in calendar.monthcalendar(2024,10) if 0 not in week]
['7-13', '14-20', '21-27']
3 Likes

While this is correct, it doesn’t address my worry directly. I am talking about a function that should handle those edge cases. You can even specify the year as well. Handle the year as a string and handle it as an integer as well
But that shorthand looks good too

The point is, the best way to handle the edge cases depends on the application. That’s what makes this function unsuitable for the stdlib - it would have to handle all the options people might want. When the number of options is low, and the complexity is high, that’s a reasonable candidate for the stdlib. But in this case, the opposite is true.

1 Like

I am talking about a function that should handle those edge cases. You can even specify the year as well. Handle the year as a string and handle it as an integer as well

If there’s nothing on PyPI already, your idea would make a nice helper library. There is demand from people, for all sorts of calendars.

A helper library? Could you explain abit more on that? or it generally represents writing utility functions that can be used everywhere?

A bit like how dateutils and more-itertools build on datetime and itertools respectively. Your own or someone else’s library could provide common shortcuts and examples for calendar.

Oh yeah, got it. That sounds like a good idea
Thanks

1 Like