Is there a code to depict a timer to demonstrate an aircraft is 30 minutes late from its original arrival time? For example, original time of arrival is 7:35 pm and now it's 8:05 pm. Will TIMEDELTA help me with this based on the original arrival time 7:35 pm? What I am looking for in the end result will print out ("Aircraft overdue") or the text change red to alert.
Asked
Active
Viewed 34 times
1 Answers
0
Yes, you don't need to do anything very complicated here. Just subtract two datetime
objects and you will end up with a timedelta
object.
from datetime import datetime, timedelta
scheduled_arrival = datetime(2020, 6, 9, 17, 35)
actual_arrival = datetime(2020, 6, 9, 18, 5)
delta = actual_arrival - scheduled_arrival
if delta > timedelta(0): # cannot compare to bare 0, need to create timedelta object
print("Late!")
# If you want to be more specific...
print(delta) # '00:30:00'
print(f"Minutes: {int(delta.total_seconds() // 60)}") # 30
else:
print("On time or early")
See more here if you need to convert strings to datetime
objects: How to calculate the time interval between two time strings

jdaz
- 5,964
- 2
- 22
- 34