Python Online Compiler
Example: Difference Between Two Time Periods in Python using datetime
C
C++
C#
Java
Python
PHP
main.py
STDIN
Run
# DIFFERENCE BETWEEN TWO TIME PERIODS in Python using datetime from datetime import datetime # Step-1 Define two datetime values time1 = datetime(2025, 5, 1, 10, 30, 0) time2 = datetime(2025, 5, 9, 15, 45, 0) # Step-2 Get difference diff = time2 - time1 total_seconds = diff.total_seconds() # Step-3 Convert to hours, minutes, seconds hours = int(total_seconds // 3600) minutes = int((total_seconds % 3600) // 60) seconds = int(total_seconds % 60) # Step-4 Print result print(f"\nTIME DIFFERENCE: {hours} HOURS, {minutes} MINUTES, {seconds} SECONDS")
Output
Clear
ADVERTISEMENTS