It's pretty common to store date and time as a timestamp in a database. A Unix timestamp is the number of seconds between a particular date and January 1, 1970 at UTC.
We can simply use the fromtimestamp() method from the datetime module to get a date from a UNIX timestamp.
Python timestamp to datetime
Output
dt_object = 2018-12-25 09:27:53 type(dt_object) = <class 'datetime.datetime'>
Here, we have imported the datetime class from the datetime module.
Then, we used the datetime.fromtimestamp() class method which returns the local date and time (datetime object). This object is stored in the dt_object variable.
Note: We can easily create a string representing date and time from a datetime object using strftime() method.
Python datetime to timestamp
In Python, we can get timestamp from a datetime object using the datetime.timestamp() method. For example,
Output
Timestamp = 1672138646.118119
Here, the datetime.timestamp() method takes a datetime object as an argument and returns a Unix timestamp.