{"id":126972,"date":"2022-08-04T12:03:28","date_gmt":"2022-08-04T12:03:28","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=533986"},"modified":"2022-08-04T12:03:28","modified_gmt":"2022-08-04T12:03:28","slug":"how-to-convert-epoch-time-to-date-time-in-python","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/08\/04\/how-to-convert-epoch-time-to-date-time-in-python\/","title":{"rendered":"How to Convert Epoch Time to Date Time in Python"},"content":{"rendered":"\n<div class=\"kk-star-ratings kksr-auto kksr-align-left kksr-valign-top\" data-payload=\"{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;533986&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\\\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}\">\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/div>\n<h2 class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\">Problem Formulation and Solution Overview<\/h2>\n<p class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\">In this article, you&#8217;ll learn how to convert <a rel=\"noreferrer noopener\" href=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" data-type=\"URL\" data-id=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" target=\"_blank\">Epoch Time<\/a> to a Date Time representation using Python.<\/p>\n<p>On January 1st, 1970, <a rel=\"noreferrer noopener\" href=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" data-type=\"URL\" data-id=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" target=\"_blank\">Epoch Time<\/a>, 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!<\/p>\n<p><em>To make it more fun, we will calculate the time elapsed in <a rel=\"noreferrer noopener\" href=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" data-type=\"URL\" data-id=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" target=\"_blank\">Epoch Time<\/a> from its inception on <strong>January 1, 1970<\/strong>, to <strong>January 1, 1985, <\/strong>when the <a rel=\"noreferrer noopener\" href=\"https:\/\/www.historic-newspapers.com\/blog\/1985-timeline\/\" data-type=\"URL\" data-id=\"https:\/\/www.historic-newspapers.com\/blog\/1985-timeline\/\" target=\"_blank\">first mobile phone call<\/a> was made in Britain by Ernie Wise to Vodafone. This will then be converted to a Date Time representation.<\/em><\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity wp-embed-aspect-16-9 wp-has-aspect-ratio\"\/>\n<p class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio has-global-color-8-background-color has-background\"><em><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4ac.png\" alt=\"\ud83d\udcac\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Question<\/strong>: How would we write code to convert an Epoch Date to a Date Time representation?<\/em><\/p>\n<p class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\">We can accomplish this task by one of the following options:<\/p>\n<ul type=\"video\" class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<li><strong>Method 1<\/strong>: Use <a href=\"https:\/\/docs.python.org\/3\/library\/datetime.html\" data-type=\"URL\" data-id=\"https:\/\/docs.python.org\/3\/library\/datetime.html\"><code>datetime.fromtimestamp()<\/code><\/a><\/li>\n<li><strong>Method 2<\/strong>: Use <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/a-gentle-introduction-to-pythons-time-module\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/a-gentle-introduction-to-pythons-time-module\/\" target=\"_blank\"><code>time.localtime()<\/code><\/a><\/li>\n<li><strong>Method 3<\/strong>: Use <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/3\/library\/datetime.html\" data-type=\"URL\" data-id=\"https:\/\/docs.python.org\/3\/library\/datetime.html\" target=\"_blank\"><code>datetime.utcfromtimestamp()<\/code><\/a><\/li>\n<li><strong>Method 4<\/strong>: Use <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/a-gentle-introduction-to-pythons-time-module\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/a-gentle-introduction-to-pythons-time-module\/\" target=\"_blank\"><code>time.localtime()<\/code><\/a> and <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/a-gentle-introduction-to-pythons-time-module\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/a-gentle-introduction-to-pythons-time-module\/\" target=\"_blank\"><code>time.strftime()<\/code><\/a><\/li>\n<\/ul>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<h2>Method 1: Use fromtimestamp()<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This method imports the <code><a rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/3\/library\/datetime.html\" data-type=\"URL\" data-id=\"https:\/\/docs.python.org\/3\/library\/datetime.html\" target=\"_blank\">datetime<\/a><\/code> library and calls the associated <a href=\"https:\/\/docs.python.org\/3\/library\/datetime.html\" data-type=\"URL\" data-id=\"https:\/\/docs.python.org\/3\/library\/datetime.html\"><code>datetime.fromtimestamp()<\/code><\/a> function to convert <a rel=\"noreferrer noopener\" href=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" data-type=\"URL\" data-id=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" target=\"_blank\">Epoch Time<\/a> into a <strong>Local<\/strong> Date Time representation. <\/p>\n<p>To run this code error-free, install the required library. Click <a href=\"https:\/\/pypi.org\/project\/DateTime\/\" data-type=\"URL\" data-id=\"https:\/\/pypi.org\/project\/DateTime\/\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a> for installation instructions.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"4\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import datetime epoch_time = 473398200\ndate_conv = datetime.datetime.fromtimestamp(epoch_time)\nprint(date_conv.strftime('%d-%m-%Y'))<\/pre>\n<p>Above, imports the <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/3\/library\/datetime.html\" data-type=\"URL\" data-id=\"https:\/\/docs.python.org\/3\/library\/datetime.html\" target=\"_blank\"><code>datetime<\/code><\/a> library. This allows the conversion of an <a rel=\"noreferrer noopener\" href=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" data-type=\"URL\" data-id=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" target=\"_blank\">Epoch Time<\/a> integer to a readable <strong>Local <\/strong>Date Time format.<\/p>\n<p>The following line declares an <a rel=\"noreferrer noopener\" href=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" data-type=\"URL\" data-id=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" target=\"_blank\">Epoch Time<\/a> integer and saves it to <code>epoch_time<\/code>.<\/p>\n<p>Next, the highlighted line converts the <a rel=\"noreferrer noopener\" href=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" data-type=\"URL\" data-id=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" target=\"_blank\">Epoch Time<\/a> into a <strong>Local <\/strong>Date Time representation and saves it to <code>date_conv<\/code>. If output to the terminal at this point, it would display as follows:<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>1985-01-01 00:00:00<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>Finally, <code>date_conv <\/code>converts into a string using <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/a-gentle-introduction-to-pythons-time-module\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/a-gentle-introduction-to-pythons-time-module\/\" target=\"_blank\"><code>strftime()<\/code><\/a> and outputs the formatted date to the terminal.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>01-01-1985<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<h2>Method 2: Use time.localtime()<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This method imports the <code><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/category\/python-time\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/category\/python-time\/\" target=\"_blank\">time<\/a><\/code> library and calls the associated <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/a-gentle-introduction-to-pythons-time-module\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/a-gentle-introduction-to-pythons-time-module\/\" target=\"_blank\"><code>time.localtime()<\/code><\/a> function to convert <a rel=\"noreferrer noopener\" href=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" data-type=\"URL\" data-id=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" target=\"_blank\">Epoch Time<\/a> into a <strong>Local <\/strong>Date Time representation.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"4\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import time epoch_time = 473398200\ndate_conv = time.localtime(epoch_time)\nprint(date_conv)<\/pre>\n<p>Above, imports the <code><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/category\/python-time\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/category\/python-time\/\" target=\"_blank\">time<\/a> <\/code>library. This allows the conversion of an <a rel=\"noreferrer noopener\" href=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" data-type=\"URL\" data-id=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" target=\"_blank\">Epoch Time<\/a> to a readable <strong>Local<\/strong> Date Time format.<\/p>\n<p>The following line declares an <a rel=\"noreferrer noopener\" href=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" data-type=\"URL\" data-id=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" target=\"_blank\">Epoch Time<\/a> integer and saves it to <code>epoch_time<\/code>.<\/p>\n<p>Next, the highlighted line converts the <a rel=\"noreferrer noopener\" href=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" data-type=\"URL\" data-id=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" target=\"_blank\">Epoch Time<\/a> into a <strong>Local <\/strong>Date Time representation and saves it to <code>date_conv<\/code> as a <a href=\"https:\/\/blog.finxter.com\/the-ultimate-guide-to-python-tuples\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/the-ultimate-guide-to-python-tuples\/\" target=\"_blank\" rel=\"noreferrer noopener\">Tuple<\/a> as shown below:<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>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)<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>The appropriate elements will need to be accessed to format a date or time. For this example, we will construct the date.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">print(f'0{date_conv[1]}-0{date_conv[2]}-{date_conv[0]}')<\/pre>\n<p>The output is as follows:<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>01-01-1985<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube\"><a href=\"https:\/\/blog.finxter.com\/how-to-convert-epoch-time-to-date-time\/\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/plugins\/wp-youtube-lyte\/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FD2i-SmUELJI%2Fhqdefault.jpg\" alt=\"YouTube Video\"><\/a><figcaption><\/figcaption><\/figure>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<h2>Method 3: Use datetime.utcfromtimestamp<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This method imports the <code><a rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/3\/library\/datetime.html\" data-type=\"URL\" data-id=\"https:\/\/docs.python.org\/3\/library\/datetime.html\" target=\"_blank\">datetime<\/a> <\/code>library and calls the associated <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/3\/library\/datetime.html\" data-type=\"URL\" data-id=\"https:\/\/docs.python.org\/3\/library\/datetime.html\" target=\"_blank\"><code>datetime.utcfromtimestamp()<\/code><\/a> function to convert an <a rel=\"noreferrer noopener\" href=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" data-type=\"URL\" data-id=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" target=\"_blank\">Epoch Time<\/a> into a <strong>UTC<\/strong> Date Time representation. <\/p>\n<p>To run this code error-free, install the required library. Click <a href=\"https:\/\/pypi.org\/project\/DateTime\/\" data-type=\"URL\" data-id=\"https:\/\/pypi.org\/project\/DateTime\/\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a> for installation instructions.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"4\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import datetime epoch_time = 473398200\ndate_conv = datetime.datetime.utcfromtimestamp(epoch_time).strftime('%Y-%m-%d %H:%M:%S')\nprint(date_conv)<\/pre>\n<p>Above, imports the <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/3\/library\/datetime.html\" data-type=\"URL\" data-id=\"https:\/\/docs.python.org\/3\/library\/datetime.html\" target=\"_blank\"><code>datetime<\/code><\/a> library. This allows the conversion of an <a rel=\"noreferrer noopener\" href=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" data-type=\"URL\" data-id=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" target=\"_blank\">Epoch Time<\/a> integer to a readable <strong>UTC <\/strong>Date Time format.<\/p>\n<p>The following line declares an <a rel=\"noreferrer noopener\" href=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" data-type=\"URL\" data-id=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" target=\"_blank\">Epoch Time<\/a> integer and saves it to <code>epoch_time<\/code>.<\/p>\n<p>Next, the highlighted line accomplishes the following:<\/p>\n<ul>\n<li>Converts an <a rel=\"noreferrer noopener\" href=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" data-type=\"URL\" data-id=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" target=\"_blank\">Epoch Time<\/a> to a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Coordinated_Universal_Time\" data-type=\"URL\" data-id=\"https:\/\/en.wikipedia.org\/wiki\/Coordinated_Universal_Time\">UTC Date<\/a> Format.<\/li>\n<li>Converts to a Date string (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/a-gentle-introduction-to-pythons-time-module\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/a-gentle-introduction-to-pythons-time-module\/\" target=\"_blank\"><code>strftime()<\/code><\/a>) into the stated format.<\/li>\n<li>Saves the result to <code>date_conv<\/code>.<\/li>\n<\/ul>\n<p>The output is sent to the terminal.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code> 1985-01-01 03:30:00<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4a1.png\" alt=\"\ud83d\udca1\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><strong>Note: <\/strong>Universal Time (UTC) is the primary standard 24-hour time clock by which the World regulates clocks and time.<\/p>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube\"><a href=\"https:\/\/blog.finxter.com\/how-to-convert-epoch-time-to-date-time\/\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/plugins\/wp-youtube-lyte\/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FCcpZRdXVQPk%2Fhqdefault.jpg\" alt=\"YouTube Video\"><\/a><figcaption><\/figcaption><\/figure>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<h2>Method 4: Use time.localtime() and time.strftime()<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This method imports the <code><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/category\/python-time\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/category\/python-time\/\" target=\"_blank\">time<\/a><\/code> library in conjunction with the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/a-gentle-introduction-to-pythons-time-module\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/a-gentle-introduction-to-pythons-time-module\/\" target=\"_blank\"><code>time.localtime()<\/code><\/a>and <a href=\"\/\/blog.finxter.com\/a-gentle-introduction-to-pythons-time-module\/\" data-type=\"URL\" data-id=\"zhttps:\/\/blog.finxter.com\/a-gentle-introduction-to-pythons-time-module\/\" target=\"_blank\" rel=\"noreferrer noopener\"><code>time.strftime()<\/code><\/a> functions to convert <a rel=\"noreferrer noopener\" href=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" data-type=\"URL\" data-id=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" target=\"_blank\">Epoch Time<\/a> into a <strong>Local <\/strong>Date Time representation.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"4\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import time epoch_time = 473398200\ndate_conv = time.strftime('%c', time.localtime(epoch_time))\nprint('Formatted Date:', date_conv)<\/pre>\n<p>Above, imports the <code><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/category\/python-time\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/category\/python-time\/\" target=\"_blank\">time<\/a> <\/code>library. This allows the conversion of an <a rel=\"noreferrer noopener\" href=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" data-type=\"URL\" data-id=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" target=\"_blank\">Epoch Time<\/a> to a readable <strong>Local <\/strong>Date Time format.<\/p>\n<p>The following line declares an <a rel=\"noreferrer noopener\" href=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" data-type=\"URL\" data-id=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" target=\"_blank\">Epoch Time<\/a> integer and saves it to <code>epoch_time<\/code>.<\/p>\n<p>Next, the highlighted line converts the <a rel=\"noreferrer noopener\" href=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" data-type=\"URL\" data-id=\"https:\/\/www.onthisday.com\/date\/1970\/january\/1\" target=\"_blank\">Epoch Time<\/a> into a <strong>Local<\/strong> Date Time representation, converts to a string (<code>strftime()<\/code>) format and saves it to <code>date_conv<\/code>. <\/p>\n<p>The output is sent to the terminal.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code> Formatted Date: Tue Jan 1 00:00:00 1985<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<h2 id=\"block-323ff1c3-82fc-4193-9c8d-125f5d3c2efb\">Summary<\/h2>\n<p id=\"block-ebf714b3-b2f5-494e-bcd5-0cd3b3709ed7\">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.<\/p>\n<p id=\"block-7790db1b-2ed9-424b-885a-382987e3d867\">Good Luck &amp; Happy Coding!<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<h2>Programmer Humor &#8211; Blockchain<\/h2>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"280\" height=\"394\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/07\/image-31.png\" alt=\"\" class=\"wp-image-457795\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/07\/image-31.png 280w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/07\/image-31-213x300.png 213w\" sizes=\"auto, (max-width: 280px) 100vw, 280px\" \/><figcaption><em>&#8220;Blockchains are like grappling hooks, in that it&#8217;s extremely cool when you encounter a problem for which they&#8217;re the right solution, but it happens way too rarely in real life.&#8221;<\/em> <strong>source <\/strong> &#8211; <a href=\"https:\/\/imgs.xkcd.com\/comics\/blockchain.png\" data-type=\"URL\" data-id=\"https:\/\/imgs.xkcd.com\/comics\/blockchain.png\" target=\"_blank\" rel=\"noreferrer noopener\">xkcd<\/a><\/figcaption><\/figure>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) Problem Formulation and Solution Overview In this article, you&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-126972","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/126972","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=126972"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/126972\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=126972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=126972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=126972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}