Open In App
Related Articles

How to convert timestamp string to datetime object in Python?

Improve Article
Save Article
Like Article

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.

Input: "2023-07-21 10:30:45"
Output: 2023-07-21 10:30:45
Explanation: In This, We are converting a timestamp string to a datetime object in Python.

Python Timestamp String to Datetime Object

Here are different methods to convert timestamp strings to datetime objects.

  • Using strptime()
  • Using datetime.strptime()
  • Using dateutil.parser.parse()
  • Using datetime.from timestamp()

Python timestamp to Datetime using strptime()

The strftime() function is used to convert date and time objects to their string representation. It takes one or more inputs of formatted code and returns the string representation.


from datetime import datetime
timestamp_string = "2023-07-21 15:30:45"
format_string = "%Y-%m-%d %H:%M:%S"
datetime_object = datetime.strptime(timestamp_string, format_string)


2023-07-17  11:30:45

Python timestamp string to Datetime using datetime.strptime()

To get a DateTime in a particular form you can use strftime() function. You can simply use the form timestamp 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 the timestamp.


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


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

Python timestamp to Datetime using dateutil.parser.parse()

Here is an example of using Python’s the .parser.parse() . This function is a powerful and flexible tool for converting various date and time representations, including timestamps, to datetime objects. It is part of the Dateutil library.


from dateutil.parser import parse
timestamp_string = "2023-07-17  11:30:45"
datetime_object = parse(timestamp_string)


2023-07-17  11:30:45

Python timestamp to Datetime using Datetime.from timestamp()

Here, we have imported the Datetime class from the an Datetime module. Then we have used datetime.fromtimestamp() method returns a datetime an object representing the datetime corresponding to the given timestamp. 


import datetime
timestamp = 1690433696
# Convert timestamp to datetime object
dt_object = datetime.datetime.fromtimestamp(timestamp)


2023-07-27 04:54:56

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 28 Aug, 2023
Like Article
Save Article
Similar Reads
Complete Tutorials