Print all day-dates between two dates
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Printing every calendar date between two dates is a common task in reporting, scheduling, and data backfills. The core logic is simple, but real code still needs to decide whether the range is inclusive, how input strings are parsed, and how to avoid subtle bugs around time-of-day values.
Use date Objects For Day-By-Day Iteration
If you only care about calendar days, iterate with datetime.date rather than full timestamps. That removes timezone and hour-level noise from the problem.
This prints each date along with the weekday name. The <= comparison makes the range inclusive of both endpoints.
Parse Input Carefully
In real applications, dates often start as strings. Parse them once at the boundary of your program and convert them into date values before looping.
Keeping parsing separate from iteration makes the loop easier to test and reuse.
Inclusive Versus Exclusive End Dates
A lot of off-by-one bugs come from not deciding whether the end date belongs in the output. For a report that covers both March 1 and March 4, you want an inclusive loop. For a half-open interval that behaves like Python slices, you want to stop before the end date.
That version prints March 1, 2, and 3 only.
Formatting The Output
Once you have a correct date iterator, formatting becomes trivial. For example, if you want Monday 2025-03-03:
If the consumer is another program, prefer machine-friendly strings such as isoformat() instead of human-formatted text.
Returning A List Instead Of Printing
Functions are more reusable when they return data instead of printing directly.
This makes it easy to plug the result into an API response, a template, or a test assertion.
Handling Timestamps With Time Components
If the inputs are full timestamps, convert them to plain dates before iterating unless you explicitly want hour-level stepping.
This prevents confusing behavior where two timestamps on the same calendar date compare differently because of their time-of-day portion.
Why pandas Is Usually Overkill Here
Libraries such as pandas can generate date ranges, but for simple day-by-day iteration the standard library is usually clearer and easier to deploy.
That works, but pulling in pandas for a small utility function is often unnecessary unless the surrounding code is already built around DataFrames.
Common Pitfalls
The most common bug is off-by-one range logic. Another is mixing datetime values with date values and accidentally comparing timestamps instead of calendar days. Developers also frequently parse input inside the loop, which adds clutter and makes the code harder to test. If your application cares about time zones, convert the timestamps into the intended local date before stepping by whole days.
Summary
- Use
datevalues andtimedelta(days=1)for calendar-day iteration. - Decide explicitly whether the end date is inclusive or exclusive.
- Parse input once, then loop over normalized date objects.
- Return data from helper functions when possible instead of printing directly.
- Convert timestamps to plain dates before iterating if only day-level output matters.
Related reading
- Print all numbers whose nonzero digits are in ascending order
- Print all permutation in lexicographic order
- Print all unique combination of factors of a given number
- Print binary tree in BFS fashion with O1 space
- Print Specific nodes at a every level calculated by a given function
- Print two-dimensional array in spiral order
- Printing all possible subsets of a list
- Printing all possible words from a 2D array of characters

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.