Python: Convert Datetime to Timestamp and vice versa

Updated: December 21, 2022 By: Goodman Post a comment

Python is a powerful programming language that is widely used for developing web applications, games, data analysis, machine learning, and much more. One of the most important data types in Python is the datetime type. A datetime object contains both the date and the time. It is used for calculating, comparing, and manipulating dates and times.

In this article, we will learn how to convert datetime objects to timestamps and vice versa in Python 3. We will also discuss the different Python functions available for conversion, as well as some tips and examples of datetime and timestamp conversions.

What is a Timestamp?

A timestamp is an integer value that represents the number of seconds since the Unix epoch. The Unix epoch is the point in time at which the Unix operating system was created, which is January 1, 1970, at 00:00:00 UTC. The Unix timestamp is commonly used to represent dates and times in various computer systems.

Timestamps are useful for comparing and sorting dates and times, as they can be easily converted to other formats. Timestamps are also used for tracking changes in data over time, as well as for measuring time differences between events.

Datetime to Timestamp

Below are a few different techniques to turn a datetime object into a timestamp. All of them make use of standard Python modules. No third-party libraries involve.

Approach 1: Using the datetime.timestamp() method

Just import the datetime module, then use the datetime.timestamp() method to convert a datetime object to a timestamp.

Example:

from datetime import datetime, timezone

my_dt = datetime(2021, 11, 5, 15, 20, 20)

my_timestamp = my_dt.replace(tzinfo=timezone.utc).timestamp()

print(my_timestamp)

Output:

1636125620.0

Approach 2: Using the time.mktime() function

The example below uses the mktime() function from the time module to convert a datetime object into a timestamp. Note that this function assumes that the passed tuple is in local time.

import datetime
import time

# Create a datetime object
dt = datetime.datetime(2025, 12, 1, 11, 10, 5) 

# Convert to timestamp
timestamp = time.mktime(dt.timetuple())
print(timestamp) 

Output:

1764562205.0

Approach 3: Using the calendar.timegm() function

The calendar.timegm() function is handy in calculating the Unix timestamps from GMT datetime objects.

Example:

import datetime
import calendar

# Create a datetime object
dt = datetime.datetime(2023, 10, 1, 15, 0, 0) 

# Convert to timestamp
timestamp = calendar.timegm(dt.timetuple())
print(timestamp) 

Output:

1696172400

Approach 4: Doing some math operations

The datetime module provides the timedelta object, which is used to represent the difference between two datetime objects. This object can be used to calculate the number of seconds between a given datetime object and the Unix epoch.

Example:

from datetime import datetime, timedelta

my_dt = datetime(2021, 11, 6, 14, 25, 59)
my_timestamp = (my_dt - datetime(1970, 1, 1)) / timedelta(seconds=1)

print(my_timestamp)

Output:

1636208759.0

Turning Timestamp into Datetime

The datetime.fromtimestamp() method can help us get the job done quickly. The fromtimestamp() method also accepts an optional timezone argument, which can be used to convert a datetime object to a timestamp in a different timezone.

Example:

from datetime import datetime

timestamp = 1636208759.0

dt = datetime.fromtimestamp(timestamp) 

print(dt)
print(type(dt))

# If you want to convert the timestamp to UTC time, use the following code:
# dt = datetime.utcfromtimestamp(timestamp)

Output:

2021-11-06 21:25:59
<class 'datetime.datetime'>

Conclusion

In this article, you have learned how to convert datetime to timestamp and vice versa using Python. We hope that this article was helpful to you. If you have any questions related to what we’ve discussed, feel free to ask in the comment section. Have a nice day, and happy coding!