Skip to content
Araz Shah
Menu
  • Home
  • About me
  • Contact me
  • CV
  • Online Courses
    • Apply Now !
    • In-Depth
    • Courses
      • Concepts
      • Python Course
      • GIS Developer Course
    • Price
Menu

iterating over dates in Python

Posted on October 5, 2023 by admin

let’s create a real-world example of iterating over dates in Python. In this example, we’ll write a program that generates a list of dates within a specific range and then performs some action for each date. For simplicity, let’s say we want to print the dates in a given month. We will use the datetime module in Python for date manipulation.

Here are the steps:

  1. Import the datetime module: First, you need to import the datetime module, which provides classes for manipulating dates and times.
import datetime
  1. Define the start and end dates: Specify the range of dates you want to iterate over. For this example, let’s choose a month, say, October 2023.
start_date = datetime.date(2023, 10, 1)
end_date = datetime.date(2023, 10, 31)
  1. Create a loop to iterate over dates: You can use a for loop to iterate over dates from the start date to the end date.
current_date = start_date
while current_date <= end_date:
    print(current_date)
    current_date += datetime.timedelta(days=1)
  • current_date is initialized to the start_date.
  • The loop continues as long as current_date is less than or equal to end_date.
  • Within the loop, we print the current_date.
  • We then increment current_date by one day using datetime.timedelta(days=1).
  1. Run the program: When you run this program, it will print all the dates from October 1, 2023, to October 31, 2023.

Here’s the complete code:

import datetime

start_date = datetime.date(2023, 10, 1)
end_date = datetime.date(2023, 10, 31)

current_date = start_date
while current_date <= end_date:
    print(current_date)
    current_date += datetime.timedelta(days=1)

When you run this code, you will get the following output, which shows all the dates in October 2023:

2023-10-01
2023-10-02
2023-10-03
...
2023-10-30
2023-10-31

This is a simple example of how to iterate over dates in Python and perform some action for each date in a real-world scenario. You can modify the code to perform different actions based on your specific needs.

Category: python, Tutorials

Post navigation

← Python Linked lists
Parsing a string date in python →

Recent Posts

  • Geospatial Risk Assessment: A Python Approach
  • Analyzing Employee Arrival Patterns and Delays Using Geospatial Data
  • Real-Time GPS Tracking on a Web Map using FastAPI & Leaflet
  • How to Create a Simple WebGIS with FastAPI, PostGIS, and Leaflet.js
  • Graph Coloring: How Many Colors Do You Need?

Archives

  • May 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • September 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • September 2023
  • August 2023
  • April 2023

Categories

  • Courses
  • Events
  • GIS
  • Linux
  • News
  • programming
  • python
  • Tutorials
  • Videos
  • May 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • September 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • September 2023
  • August 2023
  • April 2023
  • Courses
  • Events
  • GIS
  • Linux
  • News
  • programming
  • python
  • Tutorials
  • Videos

Araz Shahkarami

I’m a software enthusiast with a deep love for crafting robust and efficient solutions. My journey into the world of programming began several years ago when I was introduced to the world of code. Since then, I’ve been on an exhilarating ride of learning, problem-solving, and continuous improvement.

© 2025 Araz Shah | Powered by Minimalist Blog WordPress Theme