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 :: Dynamically limiting queryset of related field 
Python :: how to make a relationship in python 
Python :: df select custom index 
Python :: convertir code python en java 
Python :: Python - Comment préparer la capitalisation 
Python :: df filter by multiple rules python 
Python :: how to convert comma separated string to list in python 
Python :: pause and resume threads python 
Python :: Parsing a url for IP address using python 
Python :: what exception occurs when you convert a atring to an integer and fail in python 
Python :: discord.py custom status 
Python :: Flask migration method, see the artcle for more info 
Python :: python use var in another function 
Python :: how to filter csv file by columns 
Python :: afkastiningsgrad 
Python :: Get timestamp with pyrhon 
Python :: Python Print Variable Using the + operator to join variables 
Python :: create image tkinter set active background 
Python :: Adding new fields in ModelAdmin with fieldsets to edit user, and add_fieldsets whan creating a new user 
Python :: leetcode 206 python 
Python :: torch.unsqueze 
Python :: Print in python capital p 
Python :: take space away from strings ion pyhton 
Python :: find no of iterations in python 
Python :: get maximum values in a column by a subgroup of a dataframe pandas 
Python :: python three periods 
Python :: COLLECTING 
Python :: pandas dexcribe only one column 
Python :: how to serial print line break 
Python :: convert days hours minutes into seconds python 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =