
A date-based progress bar is a visual representation of the time elapsed in a project relative to its total duration, typically displayed as a percentage or a fraction of completion.
In this tutorial, we’ll explore how to create date-based progress bars for project timelines using Python. We’ll focus on using the datetime module and tqdm library to create both static and dynamic progress bars in the console. This skill is particularly useful for those who need to track and visualize project progress over time.
Prerequisites
Before we begin, make sure you have the following installed:
pip install tqdm
Let’s briefly explain the libraries we will be using to create date-based progress bars:
- datetime: This is a built-in Python module for working with dates and times. It provides classes for manipulating dates, times, and time intervals. We’ll use it to handle date calculations in our project timeline. It is part of Python’s standard library and does not need to be installed via pip.
- tqdm: Short for “taqaddum” (Arabic for “progress”), tqdm is a fast, extensible progress bar library for Python. It provides a smart progress meter for loops and other iterables, making it easy to create text-based progress bars in the console.
1. Basic Date Manipulation with datetime
Let’s start by understanding how to work with dates using Python’s built-in datetime module. In this example, we’ll demonstrate how you can set your own project start and end dates, giving you the flexibility to adapt this code to any project timeline.
from datetime import datetime, timedelta
# Get current date
current_date = datetime.now()
print(f"Current date: {current_date}")
# Create specific dates for your project
# You can change these dates to match your own project timeline
project_start = datetime(2024, 1, 1)
project_end = datetime(2024, 12, 31)
print(f"Project start date: {project_start}")
print(f"Project end date: {project_end}")
# Calculate project duration
project_duration = project_end - project_start
print(f"Project duration: {project_duration.days} days")
# Calculate days elapsed
days_elapsed = (current_date - project_start).days
print(f"Days elapsed: {days_elapsed}")
# Calculate progress percentage
progress_percentage = (days_elapsed / project_duration.days) * 100
print(f"Project progress: {progress_percentage:.2f}%")
This code demonstrates basic date operations and calculates project progress. The output will look similar to this:
Current date: 2024-09-02 14:30:45.123456 Project start date: 2024-01-01 00:00:00 Project end date: 2024-12-31 00:00:00 Project duration: 365 days Days elapsed: 245 Project progress: 67.12%
Note that the actual values will depend on the current date when you run the code and the project start and end dates you choose. Feel free to modify the project_start and project_end dates to match your specific project timeline. This flexibility allows you to use this code for any project, regardless of its duration or start date.
2. Creating a Simple Static Progress Bar
Now, let’s create a simple static progress bar using the tqdm library. This provides a visual representation of progress in the console.
from datetime import datetime
from tqdm import tqdm
def create_progress_bar(start_date, end_date):
current_date = datetime.now()
total_days = (end_date - start_date).days
elapsed_days = (current_date - start_date).days
with tqdm(total=total_days, unit="day", desc="Project Progress") as pbar:
pbar.update(elapsed_days)
print(f"\nProject is {(elapsed_days/total_days)*100:.2f}% complete.")
# Example usage
start_date = datetime(2024, 1, 1)
end_date = datetime(2024, 12, 31)
create_progress_bar(start_date, end_date)
This code creates a text-based progress bar in the console. The output will look something like this:

The progress bar visually represents the project’s completion, and the percentage is calculated based on the current date.
3. Console-Based Updating Progress Bar
For long-running projects, you might want to create a progress bar that updates regularly. Here’s an example of a dynamic progress bar:
from datetime import datetime, timedelta
from tqdm import tqdm
import time
def get_project_progress(start_date, end_date):
current_date = datetime.now()
total_days = (end_date - start_date).days
elapsed_days = (current_date - start_date).days
return min(elapsed_days, total_days), total_days # Ensure progress doesn't exceed total days
def update_progress_bar(start_date, end_date, duration_seconds=10):
with tqdm(total=100, desc="Project Progress", unit="%") as pbar:
start_time = time.time()
while time.time() - start_time < duration_seconds:
elapsed, total = get_project_progress(start_date, end_date)
progress_percentage = (elapsed / total) * 100
pbar.n = int(progress_percentage)
pbar.refresh()
time.sleep(0.1) # Update every 0.1 seconds
# Example usage
start_date = datetime(2024, 1, 1)
end_date = datetime(2024, 12, 31)
elapsed_days, total_days = get_project_progress(start_date, end_date)
print(f"Project duration: {total_days} days")
print(f"Current progress: {elapsed_days} days elapsed")
print("Updating progress bar:")
update_progress_bar(start_date, end_date)
This code creates a console-based updating progress bar. When you run it, you’ll see:
- The total project duration in days
- The current progress in days elapsed
- A progress bar that updates in real-time for 10 seconds
- The progress bar shows the percentage complete
Here’s what the output might look like:

Key features and explanations:
- The get_project_progress function calculates the elapsed days and total days based on the start date, end date, and current date.
- The update_progress_bar function creates a progress bar that updates for a specified duration (default 10 seconds).
- We use a while loop to continuously update the progress bar, refreshing it every 0.1 seconds.
- The progress is calculated as a percentage and capped at 100% to prevent it from exceeding the total.
This approach provides a simple, real-time visualization of project progress. It’s especially useful for scripts that run in environments without graphical displays or for quick progress checks in the console.
Conclusion
In this tutorial, we’ve explored various methods to create date-based progress bars for project timelines using Python. We started with basic date manipulation using the datetime module, emphasizing the flexibility to set custom project start and end dates. We then moved on to creating both static and dynamic text-based progress bars using tqdm.
These techniques can be useful for anyone who needs to visualize and track the progress of their projects over time. By incorporating these progress bars into your workflows, you can better manage project timelines and communicate progress to collaborators effectively. Remember to adjust the start and end dates in the examples to match your specific project timeline. This flexibility allows you to apply these techniques to any project, regardless of its duration or start date.
