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.
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: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.
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'>
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 D ateutil library.
Python3
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:45
Python 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.
Python3
import datetime
timestamp = 1690433696
dt_object = datetime.datetime.fromtimestamp(timestamp)
print(dt_object)
Output
2023-07-27 04:54:56
Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills and become a part of the hottest trend in the 21st century.
Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
28 Aug, 2023
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...