Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to convert timestamp string to datetime object in Python?

  • Difficulty Level : Medium
  • Last Updated : 29 Aug, 2020

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.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

Syntax:



fromtimestamp(timestamp, tz=None)

Example: Python timestamp to DateTime

Python3




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:

Python3




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

Output:

date: 24-03-19



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!