Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

how to loop over all dates in python

# this DOES account for leap years
years = ['2023', '2024', '2028']
months = ['01', '02', '03', '04', '05', '06',
              '07', '08', '09', '10', '11', '12']
days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

for i in range(len(years)):
    for j in range(len(months)):
        if (((int(years[i]) % 4 == 0 and int(years[i]) % 100 != 0) or (int(years[i])% 400 == 0)) and months[j] == '02'):
            for k in range(1, days[j] + 2):
                print(years[i] + '-' + months[j] + '-' + str(k))
        else:
            for k in range(1, days[j] + 1):
                print(years[i] + '-' + months[j] + '-' + str(k))
            

#2023-01-1
#2023-01-2
#2023-01-3
#...
Source by riptutorial.com #
 
PREVIOUS NEXT
Tagged: #loop #dates #python
ADD COMMENT
Topic
Name
8+9 =