How to find all week numbers that are the first week of a month in a year python

I want to create an event that falls on Sunday every first week of months.

This is what I am doing for a specific week number, which is the first week june

, to get the date:

from datetime import datetime
myDate = "2017 22 0"
# here `2017` is year, `22` is week number and `0` =sunday is week day
datetime.strptime(myDate, "%Y %W %w")

#output...
datetime.datetime(2017, 6, 4, 0, 0)

      

So I need a list of all week numbers that are the first week of the month, so I can loop over it and get the wish dates.

Adding additional information

I want a method that returns a list of weekly numbers that are the first week of the month, where the first week means the week has the first Sunday.

def get_week_number(year=2017, day=0):
   #day 0 means sunday
   ...
   ...
   return [1, 5, 9, 13, 18, 22, ...]  

      

+3


source to share


2 answers


I looped through the months and then got the first Sunday (starting at month 1 st and proceeding to the next day until Sunday is found) and then got the week - year from the day:

from datetime import datetime
from datetime import date

# dayofweek: Sunday=0, Monday=1 and so on
def get_week_number(year=2017, dayofweek=0):
    weeks = []

    for month in range(1, 13):
        # get first Sunday of month
        d = date(year, month, 1)
        while(d.weekday() != dayofweek):
            d = d.replace(day=d.day + 1)

        # isocalendar()[1] is the week-of-year field
        weeks.append(d.isocalendar()[1])

    return weeks

print(get_week_number(2017, 0))

      

The result, however, is different from what is expected:



[1, 6, 10, 14, 18, 23, 27, 32, 36, 40, 45, 49]

      

I also tried with weeks.append(int(d.strftime("%W")))

, but it gives the same results - I am using Python 3.5.2 and the week is defined as :

An ISO year consists of 52 or 53 full weeks and where the week starts on Monday and ends on Sunday. The first week of the ISO year is the first (Gregorian) calendar week of the year that contains Thursday. This is called week number 1, and the ISO year of this Thursday is the same as its Gregorian year.

+1


source


(Edited due to error in weekday numbering)

>>> import datetime
>>> june1 = datetime.datetime(2017,6,1)
>>> june1
datetime.datetime(2017, 6, 1, 0, 0)
>>> june1_weekday = june1.weekday()
>>> if june1_weekday < 6:  # 6 indicates Sunday
        first_sunday_in_june = june1 + datetime.timedelta(days=6-june1_weekday)
else:
        first_sunday_in_june = june1


>>> print(first_sunday_in_june)
2017-06-04 00:00:00

      

Assuming you want IDOs to be displayed during the week, you can use the method isocalendar()

. This gives a tuple (year, weeknumber, weekday)

. This uses the convention that weeks start on Monday, and the first week of the year is the first week with at least four days in the year (or in other words, the week with the first Thursday).



>>> first_sunday_in_june.isocalendar()
(2017, 22, 7)

      

If you have a different convention for the first day of the week or the first week of the year, you will need to brew your own function to get the week number.

Use the above method to cycle through months and you can create the desired list with week numbers.

+2


source







All Articles