Converting epoch time to datetime is a common task in Python. In this tutorial, we will learn how to perform this conversion in different scenarios. This tutorial is comprehensive and covers Python epoch time, converting epoch to datetime with timezone, and converting epoch milliseconds to datetime in Python.
What is Epoch Time in Python
Epoch time, also known as UNIX time, is a system for tracking time that represents the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC) on Thursday, 1 January 1970, excluding leap seconds. It is widely used in computer systems for recording time.
In Python, you can obtain the current epoch time using the time
module:
import time
epoch_time = time.time()
print(f"Current epoch time: {epoch_time}")
Basic Conversion from Epoch to Datetime in Python
To convert epoch time to datetime in Python, we can use the datetime
module. This module has a method called fromtimestamp()
which takes an epoch timestamp as input and returns a datetime object.
Example: Convert Epoch to Datetime
import time
from datetime import datetime
# Getting current epoch time
epoch_time = time.time()
# Converting epoch time to datetime
datetime_object = datetime.fromtimestamp(epoch_time)
print(f"Epoch time: {epoch_time}")
print(f"Datetime: {datetime_object}")
You can see the output in the screenshot below:
Python Convert Epoch to Datetime with Timezone
When converting epoch time to datetime, you may also need to consider time zones. Python’s datetime
module supports time zones through the pytz
library. To convert epoch time to datetime with timezone, you can use the fromtimestamp()
method of the datetime
module, with the tz
parameter.
Example: Convert Epoch to Datetime in a Specific Timezone
import time
from datetime import datetime
import pytz
# Getting current epoch time
epoch_time = time.time()
# Define a timezone
timezone = pytz.timezone('US/Eastern')
# Convert epoch to datetime with timezone
datetime_with_timezone = datetime.fromtimestamp(epoch_time, tz=timezone)
print(f"Epoch time: {epoch_time}")
print(f"Datetime with timezone: {datetime_with_timezone}")
You can see the screenshot below:
Convert Epoch Milliseconds to Datetime in Python
Sometimes, the epoch time might be given in milliseconds instead of seconds. Since fromtimestamp()
expects the epoch time in seconds, you will need to convert milliseconds to seconds before converting it to datetime in Python.
Example: Convert Epoch in Milliseconds to Datetime
from datetime import datetime
# Epoch time in milliseconds
epoch_milliseconds = 1627939200000
# Convert milliseconds to seconds
epoch_seconds = epoch_milliseconds / 1000.0
# Convert epoch time to datetime
datetime_object = datetime.fromtimestamp(epoch_seconds)
print(f"Epoch time in milliseconds: {epoch_milliseconds}")
print(f"Datetime: {datetime_object}")
Check out the screenshot below:
Conclusion
This tutorial has provided you with comprehensive knowledge on how to convert epoch time to datetime in Python. We covered how to perform basic conversions using the fromtimestamp()
method of the datetime
module, and also delved into converting epoch time with timezones and converting epoch milliseconds to datetime.
You may also like:
- Convert Integers to Datetime in Pandas in Python
- Convert DateTime to UNIX timestamp in Python
- convert a String to DateTime in Python
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.