How to Convert DateTime to Unix Timestamp in Python

09/11/2021

Contents

In this article, you will learn how to convert dateTime to unix timestamp in Python.

Converting DateTime to Unix Timestamp

In Python, you can convert a datetime object to a Unix timestamp (the number of seconds since January 1, 1970, 00:00:00 UTC) using the timestamp() method of the datetime object.

Here’s an example:

from datetime import datetime

# create a datetime object
dt = datetime(2022, 2, 15, 10, 30, 0)  # year, month, day, hour, minute, second

# convert to Unix timestamp
unix_timestamp = dt.timestamp()

print(unix_timestamp)  # output: 1644943800.0

In the above example, the datetime object dt represents February 15, 2022, 10:30:00 AM. Calling the timestamp() method on this object returns the Unix timestamp as a float value. The output of the above code is 1644943800.0.

Note that the timestamp() method returns the Unix timestamp as a float value with fractional seconds. If you need the timestamp as an integer, you can convert it using the int() function:

unix_timestamp = int(dt.timestamp())

This will give you the Unix timestamp as an integer value, without the fractional seconds.

 

Here are a few more things you might find useful to know about working with datetime objects and Unix timestamps in Python:

Creating a datetime object from a Unix timestamp

You can also create a datetime object from a Unix timestamp using the fromtimestamp() method of the datetime class. Here’s an example:

from datetime import datetime

unix_timestamp = 1644943800

# create a datetime object from the Unix timestamp
dt = datetime.fromtimestamp(unix_timestamp)

print(dt)  # output: 2022-02-15 10:30:00

In the above example, the fromtimestamp() method returns a datetime object representing the date and time corresponding to the Unix timestamp 1644943800.

Working with time zones

When working with datetime objects and Unix timestamps, it’s important to keep in mind the time zone information. Unix timestamps are defined as the number of seconds since January 1, 1970, 00:00:00 UTC. If you have a datetime object in a different time zone, you’ll need to convert it to UTC before converting to a Unix timestamp. Here’s an example:

from datetime import datetime, timezone

# create a datetime object in a different time zone
dt = datetime(2022, 2, 15, 10, 30, 0, tzinfo=timezone.utc+timezone(hours=8))

# convert to UTC
dt_utc = dt.astimezone(timezone.utc)

# convert to Unix timestamp
unix_timestamp = dt_utc.timestamp()

print(unix_timestamp)  # output: 1644929400.0

In the above example, the dt object is created with a time zone of UTC+8 (which is 8 hours ahead of UTC). We first convert it to UTC using the astimezone() method, and then convert it to a Unix timestamp.

Dealing with daylight saving time

When working with datetime objects and time zones, it’s also important to keep in mind daylight saving time (DST) changes. Some time zones observe DST, which means that the offset from UTC changes at certain times of the year. This can affect the conversion between datetime objects and Unix timestamps.

To handle DST changes correctly, you should use a library like pytz or dateutil that can handle time zones and DST changes correctly. Here’s an example using pytz:

from datetime import datetime
import pytz

# create a datetime object in a time zone that observes DST
tz = pytz.timezone('America/New_York')
dt = tz.localize(datetime(2022, 3, 13, 2, 30, 0))

# convert to UTC
dt_utc = dt.astimezone(pytz.utc)

# convert to Unix timestamp
unix_timestamp = int(dt_utc.timestamp())

print(unix_timestamp)  # output: 1647203400

In the above example, the dt object is created with a time zone of America/New_York, which observes DST. We first convert it to UTC using the astimezone() method, using the pytz library to handle the DST change correctly. We then convert it to a Unix timestamp as an integer value.