Time Between Two Timestamps

How To Calculate Time Between Two Timestamps in Python

In this tutorial, we will learn How To Calculate time between two timestamps in Python. A timestamp is a representation of date and time information typically used in UNIX systems to indicate when a specific event occurred. Let’s explore how to calculate the difference between two timestamps in hours, minutes, and seconds using Python.

To begin, assign the starting timestamp to the variable ‘start’ and the ending timestamp to the variable ‘end’.

Then, utilize the fromtimestamp() method to convert both the start and end timestamps into datetime objects. The conversion to datetime is necessary as we want to perform arithmetic operations on the timestamps.

Subsequently, subtract the datetime object representing the end timestamp from the datetime object representing the start timestamp. This subtraction will yield the time duration between the two timestamps in the form of a timedelta object.

Finally, to obtain the difference in seconds, you can employ the total_seconds() method on the timedelta object.

Example:

from datetime import datetime

# Define the start and end timestamps
start_timestamp = 1625143200  # Example: Unix timestamp for July 1, 2021, 10:00:00 AM
end_timestamp = 1625150400  # Example: Unix timestamp for July 1, 2021, 12:00:00 PM

# Convert timestamps to datetime objects
start_datetime = datetime.fromtimestamp(start_timestamp)
end_datetime = datetime.fromtimestamp(end_timestamp)

# Calculate the time difference
time_difference = end_datetime - start_datetime

# Get the difference in seconds
difference_in_seconds = time_difference.total_seconds()

# Print the time difference in hours, minutes, and seconds
hours = difference_in_seconds // 3600
minutes = (difference_in_seconds % 3600) // 60
seconds = difference_in_seconds % 60

print("Time difference:", int(hours), "hours,", int(minutes), "minutes,", int(seconds), "seconds")


Output:

Time difference: 2 hours, 0 minutes, 0 seconds

In this example, we define the starting and ending timestamps as Unix timestamps (the number of seconds since January 1, 1970). We convert these timestamps to datetime objects using the fromtimestamp() method. By subtracting the start datetime from the end datetime, we obtain the time duration as a timedelta object. Finally, we use the total_seconds() method to get the difference in seconds, and then calculate the difference in hours, minutes, and seconds accordingly.

Check out My Latest post on Developer Helps for some Interesting Insights


List of Some Important Format Specifiers

There are a few format specifiers like below :

  • %a indicates the day in three letter format
  • %d indicates day in numeric format
  • %b indicates month in three letter format
  • %Y indicates year in yyyy format
  • %H indicates hour in hh format
  • %M indicates minutes in mm format
  • %S indicates seconds in ss format
  • %z indicates the timezone in +/- xxxx format

Calculate the Difference Between two Timestamps in Seconds

Example:

from datetime import datetime

# Define the start and end timestamps
start_timestamp = datetime(2023, 7, 1, 10, 30, 0)  # Example: July 1, 2023, 10:30:00
end_timestamp = datetime(2023, 7, 1, 11, 0, 0)  # Example: July 1, 2023, 11:00:00

# Calculate the time difference in seconds
time_difference = (end_timestamp - start_timestamp).total_seconds()

# Print the time difference in seconds
print("Time difference:", int(time_difference), "seconds")


Output:

Time difference: 1800 seconds

In this program, we define the start and end timestamps using the datetime module. The timestamp is represented as datetime objects.

To calculate the difference between the two timestamp in seconds, we subtract the start timestamp from the end timestamp. The resulting difference is a timedelta object.

By calling the total_seconds() method on the timedelta object, we obtain the difference in seconds as a floating-point value. We convert it to an integer using int() for a cleaner output.

Finally, we print the time difference in seconds.

The output shows a time difference of 1800 seconds (30 minutes) between the two timestamps.

Discover Our Exciting Courses and Quiz

Enroll now to enhance your skills and knowledge!

Python Online Quiz

Level up your coding skills with our interactive programming quiz!