Convert Datetime to Unix Timestamp in Python

Dates and Unix Timestamp

Dates can be a tricky value-type to handle in programming. In Python, we have the datetime library that provides objects of datetime class that can store and processes the date values efficiently. Let us now discuss the Unix timestamp.

When computer systems were introduced it became a necessity to follow a standard time standard that can be uniform for all systems. The Unix timestamp (also known as the Unix Epoch Timestamp) is termed as the total seconds passes since the first of January, 1970.

Ways to convert datetime to Unix timestamp

Let us now discuss the methods to convert datetime to Unix timestamp in Python.

Using the total_seconds() function to convert datetime to Unix timestamp in Python

In the datetime library, we use the timedelta class to represent specific time intervals. The total_seconds() function is a function of this class that represents the total seconds in the given timedelta object.

We can use this function to convert datetime to Unix timestamp in Python. We will create a datetime object and subtract it with another datetime object that has the date 1st January 1970. The total_seconds() function will return the total seconds in this interval, which is essentially the Unix timestamp of the created datetime object.

See its implementation below.

Output:

1654878015.0

In the above example, the d1 object is the datetime object which we want to convert to the Unix timestamp.

Note that we cannot subtract offset-aware datetime objects with offset-naïve objects. To ensure this does not happen, we added the tzone attribute in each of the objects in the above example.

Using the timestamp() function to convert datetime to Unix timestamp in Python

The timestamp() function was introduced in version 3.3 of the datetime library. timestamp() function returns the corresponding Unix timestamp for a given datetime object.

For example,

Output:

1654878015.0

Using the strftime() function to convert datetime to Unix timestamp in Python

The strftime() function can be used to get a string representation of a given datetime object. We can specify the format in which we want to get the final string within the function.

We can use this to convert datetime to Unix timestamp in Python. In Python 3.3 and below, we can use the %s format to get the Unix timestamp of a given object.

See the following example,

Output:

1654878015

Using the time.mktime() function to convert datetime to Unix timestamp in Python

The datetime objects are easily manipulated using other libraries that can work with date and time values like calendar and time. We can use methods from this library as well to convert datetime to Unix timestamp in Python.

The timetuple() function of the datetime class is used to return a time.struct_time object. It fills all the necessary attributes of this object. We can use the time.mktime() function with this object to get the seconds passed since the epoch till this object.

See the code below.

Output:

1654878015.0

Using the calendar.timegm() function to convert datetime to Unix timestamp in Python

As discussed previously, the calendar function works well with datetime objects. We can use the calendar.timegm() function to convert datetime to Unix timestamp in Python.

This function works similarly to the previous method in returning the Unix timestamp. The only difference is that it considers the datetime object is in UTC/GMT time.

For example,

Output:

1654878015

Using the arrow.timestamp() function to convert datetime to Unix timestamp in Python

The arrow library is a recently introduced library that has features to handle date and time values more efficiently. It has easy-to-use functions that have combined the features from different libraries.

The timestamp() function can be used to return the Unix timestamp for a given date and time. We can pass the datetime object to the arrow.get() function and pass its returned object to the timestamp() method.

See the code below.

Output:

1654878015.0

Conclusion

In this tutorial, we discussed how to convert datetime to Unix timestamp in Python. For this, we discussed several methods involving the datetime, calendar, and time libraries.

The first three methods use the datetime library. In the first method, we manually calculate the difference between the required date and the epoch and return the total seconds between this interval using the total_seconds() function. The timestamp() and strftime() functions in the next methods directly return the Unix timestamp.

We also use methods from other libraries. We get a time.struct_time instance from a datetime object and use the time.mktime() and calendar.timegm() methods to get the Unix timestamp from this created instance. We also used the arrow library to convert datetime to Unix timestamp in Python.

That’s all about how

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *