Generating a List of Weeks for Multiple Years in Python

Introduction:

Problem:

Solution:

Code:

import datetime

def generate_weeks_year_list(length):
current_week = datetime.datetime.now().isocalendar()[1]
week_list = []
for i in range(current_week, current_week + length):
current_year = datetime.datetime.now().year + (i-1) // 52
first_day_of_year = datetime.date(current_year, 1, 1)
last_day_of_year = datetime.date(current_year, 12, 31)
weeks_in_current_year = 52
if (first_day_of_year.weekday() == 0 and last_day_of_year.weekday() == 1):
weeks_in_current_year = 51
elif (current_year % 4 == 0 and current_year % 100 != 0) or current_year % 400 == 0:
weeks_in_current_year = 53
next_week = i % weeks_in_current_year + 1
week_list.append(next_week)
return week_list

--

--

I am an entrepreneur and web developer , currently living in the Netherlands. My tools of choice are Python with Django and some good old vanilla JavaScript.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
HeySander

I am an entrepreneur and web developer , currently living in the Netherlands. My tools of choice are Python with Django and some good old vanilla JavaScript.