How to Get Timestamps in Python?

In Linux and other computing devices, the “Timestamps” are used to manipulate and represent dates and times. Python supports numerous modules that are used to deal with timestamps and perform different operations. Some of the modules such as “datetime”, “time”, etc., are used to perform operations like converting Python timestamps to DateTime format, converting timestamps to strings, etc.

This Pyhton post will address various methods to get timestamps in Python.

Method 1: Using datetime Module

To manipulate date and time, the “datetime” module is used in Python. The following code fetches the timestamp using the datetime module:

Code:

import datetime
time = datetime.datetime.now()
time_stamp = time.timestamp()
print(time_stamp)

In the above code:

  • The “datetime” module is imported into the program.
  • The “datetime.datetime.now()” function gets the present date and time.
  • The “timestamp()” method is used to return the UNIX timestamp.

Output:

The above output shows the current Python timestamp.

Method 2: Using calendar Module

To perform operations related to date, month, etc., the “calendar” module is used in Python. The following code is utilized to get the timestamp:

Code:

import calendar,time
time = time.gmtime()
time_stamp = calendar.timegm(time)
print(time_stamp)

In the above code:

  • The “calendar” and “time” modules are imported at the start of the program.
  • The “time.gmtime()” function converts the expressed time in seconds since the epoch to the current time.
  • The “calendar.timegm()” function accepts the value that is returned by the “time.gmtime()” function to get the timestamp.

Output:

The above output shows the timestamp using the calendar module.

Method 3: Using time Module

Python provides a “time” module to deal with time or tasks related to time. The following code is utilized to get the timestamp:

Code:

from time import time
time_stamp = time()
print(time_stamp)

In the above code, the “time()” function of the time module is utilized to find the timestamp.

Output:

The above output shows the current timestamp.

How to Convert Python Timestamp Into datetime?

To convert timestamp into datetime format the “datetime.fromtimestamp()” function is used in Python. The below code shows the conversion of timestamp into simple datetime format and the string representation of datetime:

Code:

import datetime 
time_curt = datetime.datetime.now()
time_stamp = time_curt.timestamp()
date_time = datetime.datetime.fromtimestamp(time_stamp)
print(date_time)

In the above code:

  • The “datetime.now()” function returns the current time.
  • The “datetime.timestamp()” function converts the current time into a timestamp.
  • The “datetime.fromtimestamp(time_stamp)” function converts the given timestamp into date time format.

Output:

The above output shows the date time format of the input timestamp format.

As an alternative, we can convert this output to a string. Here is how we can accomplish this.

Code: 

import datetime 
time_curt = datetime.datetime.now()
time_stamp = time_curt.timestamp()
date_time = datetime.datetime.fromtimestamp(time_stamp)
string_datetime = date_time.strftime("%d-%m-%Y, %H:%M:%S")
print(string_datetime)

In the above code, the formatting codes are passed as an argument to the “datetime.strftime()” function to convert the given timestamp to string format.

Output:

The above output shows the string format of the given timestamp.

Conclusion

To get timestamps the “datetime” module, “calendar” module, and “time” module are used in Python. Similarly to convert timestamps into datetime the “datetime” module is used. The “time.timestamp()” function is used to get the current timestamp. Similarly the “time()” function of the time module and the “calendar.timegm(time)” function is used to get the timestamp. This Python guide provided an in-depth overview of timestamps with appropriate examples.