How to convert timestamp string to datetime object in Python?



How to convert timestamp string to datetime object in Python?

Python has a module named DateTime to work with dates and times. We did not need to install it separately. It is pre-installed with the python package itself. A UNIX timestamp is several seconds between a particular date and January 1, 1970, at UTC.

Timestamp to DateTime object

You can simply use the fromtimestamp function from the DateTime module to get a date from a UNIX timestamp. This function takes the timestamp as input and returns the corresponding DateTime object to timestamp.

 

Syntax:

 

fromtimestamp(timestamp, tz=None)

Example: Python timestamp to DateTime

from datetime import datetime
 
 
timestamp = 1545730073
dt_obj = datetime.fromtimestamp(1140825600)
 
print("date_time:",dt_obj)
print("type of dt:",type(dt_obj))

Output:

date_time: 2006-02-25 05:30:00
type of dt_obj: <class 'datetime.datetime'>

Here, we have imported the DateTime class from DateTime module. Then we have used datetime.fromtimestamp() class method which returns the local DateTime.

To get a DateTime in a particular form in you can use strftime function. The strftime() function is used to convert date and time objects to their string representation. It takes one or more input of formatted code and returns the string representation.

Example:

from datetime import datetime
 
 
timestamp = 1553367060
dt_obj = datetime.fromtimestamp(timestamp).strftime('%d-%m-%y')
 
print("date:",dt_obj)

Output:

date: 24-03-19

Last Updated on November 13, 2021 by admin

Leave a Reply

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

Recommended Blogs