. Advertisement .
. Advertisement .
The term “epoch” refers to how many seconds have passed since January 1, 1970, which is considered the epoch date. The UNIX OS was first made available in 1970. The UNIX timestamp is another name for epoch time. Python has a module called datetime that contains a variety of classes and functions for processing date and time information. Depending on the chosen format, the datetime object can be used to record some date, time, and, if necessary, the time zone. In this blog, we will introduce “How to convert epoch to datetime in Python“. Let’s read it!
What can we do to convert epoch to datetime in Python?
Method 1: Use the time module
You can convert the epoch by using the time module. There are many convenient time-related functions in the Python time module. The function offers lots of assistance for calculations involving UNIX timestamps. Let’s look at this example:
import time my_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1641417370)) print(my_time)
Method 2: Use The time.strftime()
Another way to convert epoch to datetime in python is using
time.strftime(). It is a function of the time module. The superior function of it is converting the tuple to a datetime format. Let’s learn more about this by considering the following example:
import time epoch_time = 1641417370 time_formatted = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(epoch_time)) print("Date:", time_formatted)
Date: 2022-01-06 02:46:10
Method 3: Use datetime.fromtimestamp()
An equally effective way to convert epoch to datetime in Python is utilizing datetime.fromtimestamp(). Consider an example, which shows how to convert the Python epoch to DateTime by this.
# importing the datetime package import datetime # given epoch time epoch_time = 40246871 # using the datetime.fromtimestamp() function date_time = datetime.datetime.fromtimestamp( epoch_time ) # printing the value print("Given epoch time:", epoch_time) print("Converted Datetime:", date_time )
Given epoch time: 40246871 Converted Datetime: 1971-04-12 01:11:11
Method 4: Use time.localtime()
Epoch seconds are accepted as the input of the
localtime() function, which produces a time value sequence including 9 time sequences. Let’s choose an accidental epoch time for this case.
import time epoch_time = 1554723620 time_val = time.localtime(epoch_time) print("Date in time_struct:", time_val)
Date in time_struct: time.struct_time(tm_year=2019, tm_mon=4, tm_mday=8, tm_hour=19, tm_min=40, tm_sec=20, tm_wday=0, tm_yday=98, tm_isdst=0)
localtime() returns a tuple called
struct_time() that contains the precise year, month, day, and other datetime information that the epoch time contains.
Python lends itself well to high-level systems. Nonetheless, performance would be marginally inferior to that of the native language. You still quickly gain safety, range of motion, and maintenance if you want. Individual solutions provided in these tools are some of the most fundamental for anyone who is faced with the question “How to convert epoch to datetime in Python“. If you need more assistances or have basic Python and Anaconda questions, a growing community of people is usually willing to assist you. In addition, we anticipate a more creative day filled with new ideas and code.
→ Read more: How To Convert Datetime To Epoch in Python?
Leave a comment