Convert DateTime To Unix Timestamp In Python

This tutorial will show you how to convert DateTime to Unix timestamp in Python, as well as how to convert String Date to the timestamp in Python, through the use of practical code examples.

It was in the 1960s and 1970s that the Unix operating system was first developed. The date of Unix’s launch was set to January 1, 1970, GMT (Midnight Greenwich Mean Time). 1970-01-01T00:00:00Z is the date and time format used by the ISO standards organization.

When computing the number of seconds elapsed, “Epoch Time” is the point at which the calculation begins. The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds since midnight UTC/GMT on January 1, 1970 (midnight UTC/GMT), excluding leap seconds.

How to convert DateTime to Unix Timestamp in Python?

DateTime is commonly stored in databases as a timestamp, and almost all databases support the timestamp data type as a result. Because it is easier to track which records have been added or modified in the database, it has a number of advantages over other methods.

In addition, when compared to the DateTime datatype, it takes up less space in the DB. Following our understanding of how the Unix timestamp originated and how it is calculated, we will examine how to convert the DateTime object into a Unix timestamp in Python.

1. Get Current Timestamp Using DataTime Module In Python

We first get the current date and time using the datetime module in Python. The current date and time can be passed to the datetime.now() method. Unix timestamp can be obtained using the timestamp() method.

Let us see in the below code example the usage of the datetime module in Python.

from datetime import datetime
 
#Initializing the Current Date Time Variable
getCurrDateTime = datetime.now()

#Printing The Current Date And Time
print("Time Right Now Is -  ", getCurrDateTime)
 
#Convert Current DateTime stamp To unix TimeStamp
getCurrDateTime = datetime.timestamp(getCurrDateTime)

#Printing the Unix Timestamp
print("The Current Unix Timestamp is - ", getCurrDateTime)

Output:

Time Right Now Is -   2022-04-30 17:56:26.506979
The Current Unix Timestamp is -  1651366586.506979
  • As you can see in the above code, I was able to get the current timestamp using the datetime module.
  • And then I used the current datetime stamp to convert it to UNIX timestamp format using the timestamp function present in datetime module.
Convert DateTime To Unix Timestamp In Python

2. Convert String Date and Time to Unix TimeStamp in Python

To create a datetime object from a string, we use the strptime() method. This means that a string must follow a specific format in order to be converted into a datetime object.

The strptime() function is used to convert a given string into a date object, and then the time tuple is created. For example, we can pass in the time string and get the Unix timestamp from it by using the time module’s time() method.

Let us see in the below example code the usage of the datetime module to convert string date and time to Unix TimeStamp.

#Importing Required Library
import time
import datetime
 
# date in string format
stringDate = "02/03/2022"
 
#Convert the Given Date To Time Tuple Format
getDateTimeTupleFormat = datetime.datetime.strptime(stringDate, "%d/%m/%Y").timetuple()
print("Time tuple format ",getDateTimeTupleFormat)
 
# Using mktime() Convert To Unix TimeStamp
print("The timestamp is ",time.mktime(getDateTimeTupleFormat))

Output:

Time tuple format  time.struct_time(tm_year=2022, tm_mon=3, tm_mday=2, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=61, tm_isdst=-1)
The timestamp is  1646204400.0
  • As you can see in the above code, I was able to convert the provided date to a time tuple format using the datetime.strptime() function.
  • Then I was able to convert that time tuple to a timestamp in Unix format.

Wrap Up

In Python, there are a variety of methods for converting a Datetime to a Unix timestamp. The two best approaches are to use the mktime() method in the time module if the date is passed as a string object, or to use the mktime() method in the time module if the date is passed as a number object. If we need to get the current timestamp, we can use the datetime.timestamp() method, which returns the current time.

Please let me know in the comments section if you have any problems with any of the above code examples or if you are unable to grasp the concept. I will do my best to assist you as soon as possible.

Further Read:

  1. How To Get Column Names In Pandas Dataframe
  2. Normal Distribution in Python
  3. ImportError: attempted relative import with no known parent package
  4. [Fixed] TypeError: list indices must be integers or slices, not tuple
  5. How To Replace Characters In A String In Python

Leave a Comment