Epoch time, also known as Unix time or POSIX time, is a way of representing time as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970. Converting epoch time to a human-readable date and time is a common task in programming, especially in Python. In this article, we will explore some simple and generally used methods to achieve this conversion.
How To Convert Epoch Time To Date Time In Python?
Below, are the methods of How To Convert Epoch Time To Date Time In Python
-
Using
time
Module -
Using
datetime
Module -
Using
arrow
Library -
Using P
andas
Library
Convert Epoch Time To Date Time Using time
Module
In this example, This code uses the `time` module to convert the given epoch time (1613474400) to a formatted string representing the UTC date and time in the format ‘YYYY-MM-DD HH:mm:ss’.
import time
epoch_time = 1613474400 # Replace with your epoch time
formatted_time = time.strftime( '%Y-%m-%d %H:%M:%S' , time.gmtime(epoch_time))
print (formatted_time)
|
2021-02-16 11:20:00
Convert Epoch Time To Date Time Using datetime
Module
In this example, This code utilizes the `datetime` module to convert the given epoch time (1613474400) to a UTC `datetime` object, and then formats it into a string representation (‘YYYY-MM-DD HH:mm:ss’).
from datetime import datetime
epoch_time = 1613474400 # Replace with your epoch time
dt_object = datetime.utcfromtimestamp(epoch_time)
formatted_time = dt_object.strftime( '%Y-%m-%d %H:%M:%S' )
print (formatted_time)
|
2021-02-16 11:20:00
Convert Epoch Time To Date Time Using Arrow
Library
In this example, This code uses the `arrow` library to convert the given epoch time (1613474400) to a formatted string representing the date and time in the ‘YYYY-MM-DD HH:mm:ss’ format.
import arrow
epoch_time = 1613474400 # Replace with your epoch time
formatted_time = arrow.get(epoch_time). format ( 'YYYY-MM-DD HH:mm:ss' )
print (formatted_time)
|
Output
2021-02-16 11:20:00
Convert Epoch Time To Date Time Using Pandas
Library
In this example, This code uses the `pandas` library to convert the given epoch time (1613474400) to a formatted string representing the date and time in the ‘YYYY-MM-DD HH:mm:ss‘ format. The result is stored in a DataFrame column.
import pandas as pd
epoch_time = 1613474400 # Replace with your epoch time
df = pd.DataFrame({ 'epoch_time' : [epoch_time]})
df[ 'formatted_time' ] = pd.to_datetime(df[ 'epoch_time' ], unit = 's' ).dt.strftime( '%Y-%m-%d %H:%M:%S' )
formatted_time = df[ 'formatted_time' ].iloc[ 0 ]
print (formatted_time)
|
Output
2021-02-16 11:20:00
Conclusion
In conclusion, converting epoch time to a human-readable date and time in Python can be achieved through various straightforward methods. The use of standard libraries such as time
and datetime
provides simple and effective solutions, allowing for easy conversion and formatting. Additionally, external libraries like arrow
and pandas
offer alternative approaches, offering flexibility and additional functionalities. Whether using the simplicity of built-in modules or the enhanced features of external librarie.