Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to loop through dates in python

import datetime

# The size of each step in days
day_delta = datetime.timedelta(days=1)

start_date = datetime.date.today()
end_date = start_date + 7*day_delta

for i in range((end_date - start_date).days):
    print(start_date + i*day_delta)
Comment

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
#...
Comment

how to loop over all dates in python

#this does not account for leap years
years = [str(i) for i in range(2010, 2022)]
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)):
    for k in range(1, days[j] + 1):
      print("The date is: " + years[i] + months[j] + str(k))
      



Comment

PREVIOUS NEXT
Code Example
Python :: python write text file 
Python :: jupyter notebook plot larger 
Python :: webhook discord files 
Python :: python delete folder 
Python :: extended euclidean python 
Python :: python delete saved image 
Python :: add seconds to datetime python 
Python :: load model tensorflow 
Python :: axis number size matplotlib 
Python :: python reload module without restarting 
Python :: Python project root dir 
Python :: how to find ip address of website using python 
Python :: install matplotlib.pyplot mac python 3 
Python :: download pdf from url python 
Python :: numpy array to torch tensor 
Python :: how to check if column has na python 
Python :: python flask sample application 
Python :: python delete none from list 
Python :: python get cpu cores 
Python :: degree symbol in python 
Python :: how to install drivers for selenium python 
Python :: Update all packages using pip on Windows 
Python :: python create new pandas dataframe with specific columns 
Python :: python 2.7 ubuntu command 
Python :: pd read csv unname 
Python :: get a list of column names pandas 
Python :: python hand tracking module 
Python :: tk table python 
Python :: python ping ip address 
Python :: run celery on windows 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =