How to Convert DateTime to UNIX Timestamp in Python?

Do you need to convert DateTime to a UNIX timestamp in Python? In this tutorial, I will show you how to convert DateTime to UNIX timestamp in Python.

To convert a datetime object to a Unix timestamp in Python, you can use the timestamp() method provided by the datetime module. First, create a datetime object, then call the timestamp() method on it. For example:

from datetime import datetime

dt = datetime(2024, 8, 14, 12, 0, 0)
unix_timestamp = dt.timestamp()
print(f"Unix Timestamp: {unix_timestamp}")

This will output the Unix timestamp representing the specified date and time.

Unix timestamps represent the number of seconds that have elapsed since the Unix epoch (00:00:00 UTC on 1 January 1970).

Convert datetime to Unix Timestamp

Python’s datetime module provides a powerful way to work with dates and times. To convert a datetime object to a Unix timestamp, we can use the timestamp() method. Here’s a step-by-step example:

from datetime import datetime

# Create a datetime object
dt = datetime(2024, 8, 14, 12, 0, 0)

# Convert datetime to Unix timestamp
unix_timestamp = dt.timestamp()

print(f"Unix Timestamp: {unix_timestamp}")

In this example, we first import the datetime class from the datetime module. We then create a datetime object representing August 14, 2024, at 12:00 PM. By calling the timestamp() method on this object, we obtain the Unix timestamp.

I have executed the above Python code and you can see the output in the screenshot below:

Convert DateTime to UNIX Timestamp in Python

Read Convert Python String to Datetime with Timezone

Convert datetime to Unix Timestamp with Millisecond Precision

Sometimes, you might need Unix timestamps with millisecond precision. Python’s datetime module does not directly support millisecond precision, but we can achieve it by combining the timestamp() method with additional calculations.

Here’s how you can convert a datetime object to a Unix timestamp in milliseconds in Python:

from datetime import datetime

# Create a datetime object
dt = datetime(2024, 8, 14, 12, 0, 0, 123456)

# Convert datetime to Unix timestamp with milliseconds
unix_timestamp_ms = int(dt.timestamp() * 1000)

print(f"Unix Timestamp (milliseconds): {unix_timestamp_ms}")

In this example, we create a datetime object that includes microseconds. By multiplying the result of the timestamp() method by 1000, we convert the Unix timestamp to milliseconds. Finally, we cast the result to an integer to remove any fractional milliseconds.

You can see the output in the screenshot below after I executed the above Python code.

Convert datetime to Unix Timestamp with Millisecond Precision in Python

Conclusion

In this tutorial, I have explained how to convert DateTime to UNIX Timestamp in Python and also explained convert datetime to Unix Timestamp with millisecond precision in Python.

You may like the following tutorials:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.