Last Updated : 15 Jul, 2025
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"Python Timestamp String to Datetime Object
Output: 2023-07-21 10:30:45
Explanation: In This, We are converting a timestamp string to a datetime object in Python.
Here are different methods to convert timestamp strings to datetime objects.
strptime()
datetime.strptime()
dateutil.parser.parse()
datetime.from timestamp()
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.
Python3
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)
print(datetime_object)
Output
2023-07-17 11:30:45Python 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.
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:00Python timestamp to Datetime using
type of dt_obj: <class 'datetime.datetime'>
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)
print(datetime_object)
Output
2023-07-17 11:30:45Python timestamp to Datetime using D
atetime.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)
print(dt_object)
Output
2023-07-27 04:54:56
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4