08-08-2022, 07:24 AM
How to Convert Epoch Time to Date Time in Python
5/5 – (1 vote)
Problem Formulation and Solution Overview
In this article, you’ll learn how to convert Epoch Time to a Date Time representation using Python.
On January 1st, 1970, Epoch Time, aka Time 0 for UNIX systems, started as a date in history to remember. This date is relevant, not only due to this event but because it redefined how dates are calculated!
To make it more fun, we will calculate the time elapsed in Epoch Time from its inception on January 1, 1970, to January 1, 1985, when the first mobile phone call was made in Britain by Ernie Wise to Vodafone. This will then be converted to a Date Time representation.
Question: How would we write code to convert an Epoch Date to a Date Time representation?
We can accomplish this task by one of the following options:
Method 1: Use fromtimestamp()
This method imports the library and calls the associated function to convert Epoch Time into a Local Date Time representation.
To run this code error-free, install the required library. Click here for installation instructions.
Above, imports the library. This allows the conversion of an Epoch Time integer to a readable Local Date Time format.
The following line declares an Epoch Time integer and saves it to.
Next, the highlighted line converts the Epoch Time into a Local Date Time representation and saves it to. If output to the terminal at this point, it would display as follows:
Finally,converts into a string using and outputs the formatted date to the terminal.
Method 2: Use time.localtime()
This method imports the library and calls the associated function to convert Epoch Time into a Local Date Time representation.
Above, imports thelibrary. This allows the conversion of an Epoch Time to a readable Local Date Time format.
The following line declares an Epoch Time integer and saves it to.
Next, the highlighted line converts the Epoch Time into a Local Date Time representation and saves it to as a Tuple as shown below:
The appropriate elements will need to be accessed to format a date or time. For this example, we will construct the date.
The output is as follows:
![[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]](https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FD2i-SmUELJI%2Fhqdefault.jpg)
Method 3: Use datetime.utcfromtimestamp
This method imports thelibrary and calls the associated function to convert an Epoch Time into a UTC Date Time representation.
To run this code error-free, install the required library. Click here for installation instructions.
Above, imports the library. This allows the conversion of an Epoch Time integer to a readable UTC Date Time format.
The following line declares an Epoch Time integer and saves it to.
Next, the highlighted line accomplishes the following:
The output is sent to the terminal.
Note: Universal Time (UTC) is the primary standard 24-hour time clock by which the World regulates clocks and time.
![[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]](https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FCcpZRdXVQPk%2Fhqdefault.jpg)
Method 4: Use time.localtime() and time.strftime()
This method imports the library in conjunction with the and functions to convert Epoch Time into a Local Date Time representation.
Above, imports thelibrary. This allows the conversion of an Epoch Time to a readable Local Date Time format.
The following line declares an Epoch Time integer and saves it to.
Next, the highlighted line converts the Epoch Time into a Local Date Time representation, converts to a string () format and saves it to .
The output is sent to the terminal.
Summary
These four (4) methods of converting an Epoch Time to a Date Time representation should give you enough information to select the best one for your coding requirements.
Good Luck & Happy Coding!
Programmer Humor – Blockchain
[i]“Blockchains are like grappling hooks, in that it’s extremely cool when you encounter a problem for which they’re the right solution, but it happens way too rarely in real life.” source – xkcd[/i]
5/5 – (1 vote)
Problem Formulation and Solution Overview
In this article, you’ll learn how to convert Epoch Time to a Date Time representation using Python.
On January 1st, 1970, Epoch Time, aka Time 0 for UNIX systems, started as a date in history to remember. This date is relevant, not only due to this event but because it redefined how dates are calculated!
To make it more fun, we will calculate the time elapsed in Epoch Time from its inception on January 1, 1970, to January 1, 1985, when the first mobile phone call was made in Britain by Ernie Wise to Vodafone. This will then be converted to a Date Time representation.
Question: How would we write code to convert an Epoch Date to a Date Time representation?We can accomplish this task by one of the following options:
- Method 1: Use Code:
datetime.fromtimestamp()
- Method 2: Use Code:
time.localtime()
- Method 3: Use Code:
datetime.utcfromtimestamp()
- Method 4: Use andCode:
time.localtime()Code:time.strftime()
Method 1: Use fromtimestamp()
This method imports the
Code:
datetimeCode:
datetime.fromtimestamp()To run this code error-free, install the required library. Click here for installation instructions.
Code:
import datetime epoch_time = 473398200
date_conv = datetime.datetime.fromtimestamp(epoch_time)
print(date_conv.strftime('%d-%m-%Y'))Above, imports the
Code:
datetimeThe following line declares an Epoch Time integer and saves it to
Code:
epoch_timeNext, the highlighted line converts the Epoch Time into a Local Date Time representation and saves it to
Code:
date_convCode:
1985-01-01 00:00:00Finally,
Code:
date_convCode:
strftime()Code:
01-01-1985Method 2: Use time.localtime()
This method imports the
Code:
timeCode:
time.localtime()Code:
import time epoch_time = 473398200
date_conv = time.localtime(epoch_time)
print(date_conv)Above, imports the
Code:
timeThe following line declares an Epoch Time integer and saves it to
Code:
epoch_timeNext, the highlighted line converts the Epoch Time into a Local Date Time representation and saves it to
Code:
date_convCode:
time.struct_time(tm_year=1985, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=1, tm_isdst=0)The appropriate elements will need to be accessed to format a date or time. For this example, we will construct the date.
Code:
print(f'0{date_conv[1]}-0{date_conv[2]}-{date_conv[0]}')The output is as follows:
Code:
01-01-1985Method 3: Use datetime.utcfromtimestamp
This method imports the
Code:
datetimeCode:
datetime.utcfromtimestamp()To run this code error-free, install the required library. Click here for installation instructions.
Code:
import datetime epoch_time = 473398200
date_conv = datetime.datetime.utcfromtimestamp(epoch_time).strftime('%Y-%m-%d %H:%M:%S')
print(date_conv)Above, imports the
Code:
datetimeThe following line declares an Epoch Time integer and saves it to
Code:
epoch_timeNext, the highlighted line accomplishes the following:
- Converts an Epoch Time to a UTC Date Format.
- Converts to a Date string () into the stated format.Code:
strftime()
- Saves the result to .Code:
date_conv
The output is sent to the terminal.
Code:
1985-01-01 03:30:00
Note: Universal Time (UTC) is the primary standard 24-hour time clock by which the World regulates clocks and time.Method 4: Use time.localtime() and time.strftime()
This method imports the
Code:
timeCode:
time.localtime()Code:
time.strftime()Code:
import time epoch_time = 473398200
date_conv = time.strftime('%c', time.localtime(epoch_time))
print('Formatted Date:', date_conv)Above, imports the
Code:
timeThe following line declares an Epoch Time integer and saves it to
Code:
epoch_timeNext, the highlighted line converts the Epoch Time into a Local Date Time representation, converts to a string (
Code:
strftime()Code:
date_convThe output is sent to the terminal.
Code:
Formatted Date: Tue Jan 1 00:00:00 1985Summary
These four (4) methods of converting an Epoch Time to a Date Time representation should give you enough information to select the best one for your coding requirements.
Good Luck & Happy Coding!
Programmer Humor – Blockchain
[i]“Blockchains are like grappling hooks, in that it’s extremely cool when you encounter a problem for which they’re the right solution, but it happens way too rarely in real life.” source – xkcd[/i]

