# Getting all starting dates of year 2020 with ISO week frequency

Stack Overflow Asked by umbe1987 on November 1, 2020

I’d like to create a list of dates each of which represents the starting date of ISO week N of year 2020.

Something like:

weeks2020 = [date(2020, 1, 1), date(2020, 1, 6), date(2020, 1, 13), ...]


I have obtained something similar using timedelta(weeks=1), and adding this to my START_DATE (date(2020, 1, 1)), but the dates I obtain are not correct.

I know I could simply change my START_DATE to be date(2019, 12, 30), but I would like to know if there is a more robust approach to derive all the week starting dates present in a given year.

Just for the sake of clarity, here is what i am doing now:

from datetime import date, timedelta

START_DATE = date(2020, 1, 1)
INTERVAL = timedelta(weeks=1)
STEPS = 54

prev_date = START_DATE

for i in range(1, STEPS):
print(prev_date.strftime('%Y-%m-%d')) # step 1: 2020-01-01, step 2: 2020-01-08, ...
prev_date += INTERVAL


For the first interval, find the weekday of the starting date and subtract this from a full week. After the first step, set the interval back to one week.

START_DATE = date(2020, 1, 1)
INTERVAL = timedelta(weeks=1) - timedelta(days=START_DATE.weekday())

cur_date = START_DATE

while cur_date.year == START_DATE.year:
print(cur_date.strftime("%Y-%m-%d"))
cur_date += INTERVAL
INTERVAL = timedelta(weeks=1)


Correct answer by Wups on November 1, 2020

If you can use pandas, maybe something like this should be feasible-

import pandas as pd

di = pd.to_datetime(pd.date_range(start='2020-1-1', end='2020-12-31', freq='D'))
# check for either monday or start of the month
weekstart = di[(di.weekday == 0) | ((di.day == 1) & (di.weekday != 6)]

Output
DatetimeIndex(['2020-01-01', '2020-01-06', '2020-01-13', '2020-01-20',
'2020-01-27', '2020-02-01', '2020-02-03', '2020-02-10',
'2020-02-17', '2020-02-24', '2020-03-02', '2020-03-09',
'2020-03-16', '2020-03-23', '2020-03-30', '2020-04-01',
'2020-04-06', '2020-04-13', '2020-04-20', '2020-04-27',
'2020-05-01', '2020-05-04', '2020-05-11', '2020-05-18',
'2020-05-25', '2020-06-01', '2020-06-08', '2020-06-15',
'2020-06-22', '2020-06-29', '2020-07-01', '2020-07-06',
'2020-07-13', '2020-07-20', '2020-07-27', '2020-08-01',
'2020-08-03', '2020-08-10', '2020-08-17', '2020-08-24',
'2020-08-31', '2020-09-01', '2020-09-07', '2020-09-14',
'2020-09-21', '2020-09-28', '2020-10-01', '2020-10-05',
'2020-10-12', '2020-10-19', '2020-10-26', '2020-11-02',
'2020-11-09', '2020-11-16', '2020-11-23', '2020-11-30',
'2020-12-01', '2020-12-07', '2020-12-14', '2020-12-21',
'2020-12-28'],
dtype='datetime64[ns]', freq=None)


Answered by sai on November 1, 2020

Trying to answer myself.

I don't know if this is the best solution, but it seems to work fine for my aim:

from datetime import date, timedelta

START_DATE = date(2020, 1, 1)
INTERVAL = timedelta(days=1)
STEPS = 366

iso_week = 0

weeks_2020 = []

curr_date = START_DATE

for d in range (STEPS):
if not curr_date.isocalendar()[1] == iso_week:
iso_week = curr_date.isocalendar()[1]
weeks_2020.append(curr_date)
curr_date += INTERVAL


Answered by umbe1987 on November 1, 2020

## Related Questions

### How to determine the number of negation words per sentence

2  Asked on January 3, 2021 by user14289862

### Scala getClass.getResource() returning null

4  Asked on January 3, 2021 by tower

### Attempt to invoke virtual method ‘android.content.Context.getSharedPreferences(java.lang.String, int)’ on a null object reference

1  Asked on January 2, 2021 by the-rais

### Use background image instead of border

1  Asked on January 2, 2021 by user3808307

### Appended messages collides with previous data

1  Asked on January 2, 2021 by nancy-mooree

### It is possible save node_models on gitlab ci runner?

0  Asked on January 2, 2021 by edgaras-karka

### My gRPC-Test Project just work on localhost

1  Asked on January 2, 2021 by blindmeis

### URL constructor appends ‘/’ at the end of the url

0  Asked on January 2, 2021 by ayusch-jain

### CAS server authentication using LDAP

1  Asked on January 2, 2021 by aff

### How to use static library and module map file with Swift?

1  Asked on January 2, 2021 by rob-n

### Error: Too many re-renders. React limits the number of renders to prevent an infinite loop. because of setState

1  Asked on January 2, 2021 by sudarshan-mane

### Do unsigned integers get promoted to signed? What is the type of expression (uint16_t)-1 * (uint16_t)-1

3  Asked on January 2, 2021 by user2622016

### Using the AWS EC2 Query API

1  Asked on January 2, 2021 by xliff

### JSX TypeScript definitions override for custom framework’s components?

2  Asked on January 2, 2021 by kos

### guidata not updating in MATLAB GUI

3  Asked on January 2, 2021 by matias-andina

### Change long click delay

7  Asked on January 1, 2021 by fhucho

### Dynamic parameters startWith Rxjs

1  Asked on January 1, 2021 by luden

### Better way to convert decimal float into special hexadecimal notation

2  Asked on January 1, 2021 by myz

### How to create folder (Android R – Api 30)?

3  Asked on January 1, 2021 by theo-jedi

### Passing variables from a HTML form to PHP

2  Asked on January 1, 2021 by jim-r

### Ask a Question

Get help from others!