Convert DateTime to Unix timestamp in Python

This tutorial will look at how to convert DateTime to Unix timestamp in Python and String Date to timestamp with examples.

What is Unix Timestamp?

Unix was initially developed between 1960 – and 1970. The start time of the Unix was set to January 1, 1970, GMT (Midnight Greenwich Mean Time). The ISO format is represented as ISO 8601: 1970-01-01T00:00:00Z

In Computing, “Epoch Time” refers to the starting point used to calculate the number of seconds elapsed.

The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT) and not counting the leap seconds. 

How to convert DateTime to Unix Timestamp in Python?

It is common to store the DateTime as a Timestamp in the Database, and most Databases have a timestamp data type. It has a lot of benefits as it’s easier to track the created and modified records in the Database. It also occupies lesser space in DB when compared to DateTime datatype.

Now that we know the history of the Unix timestamp and how it is calculated, let us look at how to convert the DateTime object to Unix Timestamp in Python.

Example 1 – How to get the Current timestamp in Python using datetime module?

Using the Python’s datetime module we first get the current date and time using datetime.now() method, and we can pass the current datetime to datetime.timestamp() method to obtain the Unix timestamp.

from datetime import datetime

# current date and time
currentDateTime = datetime.now()
print("Current Date Time is ", currentDateTime)

# convert datetime to timestamp
timestamp = datetime.timestamp(currentDateTime)
print("Current Unix Timestamp is ", timestamp)

Output

Current Date Time is  2022-04-23 21:39:43.821740
Current Unix Timestamp is  1650730183.82174

Example 2 – How to convert string date to timestamp in Python

We leverage the strptime() method to convert the string to a datetime object. We cannot create the datetime object from any string, meaning a string needs to be in a specific format to convert it into a datetime object.

We first convert it into a given string into a date object using the strptime() and then convert it into the time tuple.

Using the time module’s mktime() method, we can pass the time tuple to convert it into the Unix timestamp.

import time
import datetime

# date in string format
dt="23/04/2022"

# convert into the time tuple
time_tuple=datetime.datetime.strptime(dt, "%d/%m/%Y").timetuple()
print("Time tuple format ",time_tuple)

# using mktime() convert to timestamp
print("The timestamp is ",time.mktime(time_tuple))

Output

Time tuple format  time.struct_time(tm_year=2022, tm_mon=4, tm_mday=23, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=113, tm_isdst=-1)

The timestamp is  1650652200.0

Conclusion

There are multiple ways to convert Datetime to Unix timestamp in Python. The two best approaches are using the mktime() method in the time module if the date is passed as a string object. If we have to get the current timestamp, we can leverage the datetime.timestamp() method.

Leave a Reply

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

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like