Calculate percentage between starttime currenttime and endtime python

Calculating the percentage between a start time, current time, and end time in Python can be achieved in various ways. In this article, we will explore three different approaches to solve this problem.

Approach 1: Using datetime module

``````import datetime

def calculate_percentage(start_time, current_time, end_time):
start = datetime.datetime.strptime(start_time, "%H:%M:%S")
current = datetime.datetime.strptime(current_time, "%H:%M:%S")
end = datetime.datetime.strptime(end_time, "%H:%M:%S")

total_duration = end - start
elapsed_duration = current - start

percentage = (elapsed_duration.total_seconds() / total_duration.total_seconds()) * 100

return percentage

start_time = "09:00:00"
current_time = "10:30:00"
end_time = "12:00:00"

percentage = calculate_percentage(start_time, current_time, end_time)
print(f"The percentage elapsed is: {percentage}%")``````

In this approach, we use the datetime module to convert the time strings into datetime objects. We then calculate the total duration between the start and end times and the elapsed duration between the start and current times. Finally, we calculate the percentage by dividing the elapsed duration by the total duration and multiplying by 100.

Approach 2: Using time module

``````import time

def calculate_percentage(start_time, current_time, end_time):
start = time.strptime(start_time, "%H:%M:%S")
current = time.strptime(current_time, "%H:%M:%S")
end = time.strptime(end_time, "%H:%M:%S")

total_duration = time.mktime(end) - time.mktime(start)
elapsed_duration = time.mktime(current) - time.mktime(start)

percentage = (elapsed_duration / total_duration) * 100

return percentage

start_time = "09:00:00"
current_time = "10:30:00"
end_time = "12:00:00"

percentage = calculate_percentage(start_time, current_time, end_time)
print(f"The percentage elapsed is: {percentage}%")``````

In this approach, we use the time module to convert the time strings into time objects. We then calculate the total duration between the start and end times and the elapsed duration between the start and current times. Finally, we calculate the percentage using the same formula as in the previous approach.

Approach 3: Using timedelta

``````import datetime

def calculate_percentage(start_time, current_time, end_time):
start = datetime.datetime.strptime(start_time, "%H:%M:%S")
current = datetime.datetime.strptime(current_time, "%H:%M:%S")
end = datetime.datetime.strptime(end_time, "%H:%M:%S")

total_duration = end - start
elapsed_duration = current - start

percentage = (elapsed_duration / total_duration) * 100

return percentage

start_time = "09:00:00"
current_time = "10:30:00"
end_time = "12:00:00"

percentage = calculate_percentage(start_time, current_time, end_time)
print(f"The percentage elapsed is: {percentage}%")``````

In this approach, we also use the datetime module to convert the time strings into datetime objects. However, instead of using the total_seconds() method, we directly divide the elapsed duration by the total duration to calculate the percentage.

Among these three options, Approach 1 using the datetime module is the recommended solution. It provides a more straightforward and readable code, making it easier to understand and maintain. Additionally, the datetime module offers more flexibility and functionality for working with dates and times in Python.

Rate this post

7 Responses

1. Emanuel says:

Approach 2 using time module seems simpler, but does it sacrifice accuracy? 🤔

2. Willow says:

Approach 3 seems like the most efficient way to calculate percentage. What do you think? #Python #TimeCalculation

3. Jessie Guevara says:

Approach 2: Using time module seems simpler, no need for extra imports. What do you think?

4. Ira says:

Hey guys, I personally think Approach 1 is the way to go. What do you all say? 🤔

5. Alena Mcgee says:

Approach 3 is the way to go! timedelta makes calculating time intervals so much easier. #PythonPower

6. Stephen Beard says:

Approach 3 seems more efficient, but I wonder if theres a fourth approach out there? 🤔

7. Bianca Grant says:

Approach 2 seems simpler, but Approach 3 offers more flexibility. What do you think?