Convert Epoch Time to datetime Object & Vice Versa in Python (Example)
On this page, you’ll learn how to convert an epoch time into a datetime object and vice versa in the Python programming language.
This article contains the sections below:
Let’s dive into the Python code!
Example 1: Convert Epoch Time to datetime Object
This example explains how to convert an epoch time into a datetime object.
Consider the following epoch time object:
my_epoch_time1 = 4538142 print(my_epoch_time1) # 4538142
Next, we have to import the datetime module:
import datetime
To transform this epoch time to a datetime format, we have to specify the epoch time data object as a parameter within the fromtimestamp() function:
my_date_time1 = datetime.datetime.fromtimestamp(my_epoch_time1) print(my_date_time1) # 1970-02-22 12:35:42
As you can see, we have created a new datetime object containing the date and time that corresponds to our epoch time.
Example 2: Convert datetime Object to Epoch Time
This example explains how to convert a datetime object into an epoch time.
For this example, we first have to create a datetime object:
my_datetime2 = datetime.datetime(1983, 2, 17, 11, 7) print(my_datetime2) # 1983-02-17 11:07:00
In the next step, we can apply the timestamp() function to transform our datetime object to an epoch time:
my_epoch_time2 = my_datetime2.timestamp() print(my_epoch_time2) # 414324420.0
Video, Further Resources & Summary
Do you need more explanations on how to set the seconds since an epoch time to a datetime object and the other way around in Python? Then you should have a look at the following video of Pretty Printed’s YouTube channel.
The video is about parsing and formatting dates using the strftime() and strptime() functions in Python.
Furthermore, you may have a look at the other Python and datetime tutorials on this website:
- Make Unaware datetime Time Zone Aware in Python
- Convert datetime to Unix Timestamp in Python
- Convert datetime to Different Time Zone
- Convert datetime Object to Local Time Zone
- Python Programming Tutorials
This post has shown how to change an epoch time into a datetime object and vice versa in Python programming. For further questions, you may leave a comment below.
Note: This article was created in collaboration with Gottumukkala Sravan Kumar. You may find more information about Gottumukkala and his other articles on his profile page.
Subscribe to the Statistics Globe Newsletter
Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.
Thank you!
Welcome to the Statistics Globe newsletter. From now on, I’ll send you regular emails about statistics, data science, AI, and programming with R and Python.
I’m Joachim Schork. On this website, I provide statistics tutorials as well as code in Python and R programming.
Statistics Globe Newsletter
Get regular updates on the latest tutorials, offers & news at Statistics Globe. I hate spam & you may opt out anytime: Privacy Policy.
Thank you!
Please check your email inbox and click the confirmation link to complete your subscription. If you don’t see the email within a few minutes, please also check your spam/junk folder.







