[Tut] How to Convert Epoch Time to Date Time in Python - Printable Version +- Sick Gaming (https://www.sickgaming.net) +-- Forum: Programming (https://www.sickgaming.net/forum-76.html) +--- Forum: Python (https://www.sickgaming.net/forum-83.html) +--- Thread: [Tut] How to Convert Epoch Time to Date Time in Python (/thread-99782.html) |
[Tut] How to Convert Epoch Time to Date Time in Python - xSicKxBot - 08-08-2022 How to Convert Epoch Time to Date Time in Python <div> <div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload="{"align":"left","id":"533986","slug":"default","valign":"top","reference":"auto","class":"","count":"1","readonly":"","score":"5","best":"5","gap":"5","greet":"Rate this post","legend":"5\/5 - (1 vote)","size":"24","width":"142.5","_legend":"{score}\/{best} - ({count} {votes})","font_factor":"1.25"}"> <div class="kksr-stars"> <div class="kksr-stars-inactive"> <div class="kksr-star" data-star="1" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="2" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="3" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="4" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="5" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> </p></div> <div class="kksr-stars-active" style="width: 142.5px;"> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> </p></div> </div> <div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div> </div> <h2 class="wp-embed-aspect-16-9 wp-has-aspect-ratio">Problem Formulation and Solution Overview</h2> <p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">In this article, you’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> <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> <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> <hr class="wp-block-separator has-alpha-channel-opacity wp-embed-aspect-16-9 wp-has-aspect-ratio"/> <p class="wp-embed-aspect-16-9 wp-has-aspect-ratio has-global-color-8-background-color has-background"><em><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4ac.png" alt="?" 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> <p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">We can accomplish this task by one of the following options:</p> <ul type="video" class="wp-embed-aspect-16-9 wp-has-aspect-ratio"> <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> <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> <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> <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> </ul> <hr class="wp-block-separator has-alpha-channel-opacity"/> <h2>Method 1: Use fromtimestamp()</h2> <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> <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> <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 date_conv = datetime.datetime.fromtimestamp(epoch_time) print(date_conv.strftime('%d-%m-%Y'))</pre> <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> <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> <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> <figure class="wp-block-table is-style-stripes"> <table> <tbody> <tr> <td><code>1985-01-01 00:00:00</code></td> </tr> </tbody> </table> </figure> <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> <figure class="wp-block-table is-style-stripes"> <table> <tbody> <tr> <td><code>01-01-1985</code></td> </tr> </tbody> </table> </figure> <hr class="wp-block-separator has-alpha-channel-opacity"/> <h2>Method 2: Use time.localtime()</h2> <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> <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 date_conv = time.localtime(epoch_time) print(date_conv)</pre> <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> <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> <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> <figure class="wp-block-table is-style-stripes"> <table> <tbody> <tr> <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> </tr> </tbody> </table> </figure> <p>The appropriate elements will need to be accessed to format a date or time. For this example, we will construct the date.</p> <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> <p>The output is as follows:</p> <figure class="wp-block-table is-style-stripes"> <table> <tbody> <tr> <td><code>01-01-1985</code></td> </tr> </tbody> </table> </figure> <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 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> <hr class="wp-block-separator has-alpha-channel-opacity"/> <h2>Method 3: Use datetime.utcfromtimestamp</h2> <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> <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> <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 date_conv = datetime.datetime.utcfromtimestamp(epoch_time).strftime('%Y-%m-%d %H:%M:%S') print(date_conv)</pre> <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> <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> <p>Next, the highlighted line accomplishes the following:</p> <ul> <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> <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> <li>Saves the result to <code>date_conv</code>.</li> </ul> <p>The output is sent to the terminal.</p> <figure class="wp-block-table is-style-stripes"> <table> <tbody> <tr> <td><code> 1985-01-01 03:30:00</code></td> </tr> </tbody> </table> </figure> <p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" 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> <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 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> <hr class="wp-block-separator has-alpha-channel-opacity"/> <h2>Method 4: Use time.localtime() and time.strftime()</h2> <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> <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 date_conv = time.strftime('%c', time.localtime(epoch_time)) print('Formatted Date:', date_conv)</pre> <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> <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> <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> <p>The output is sent to the terminal.</p> <figure class="wp-block-table is-style-stripes"> <table> <tbody> <tr> <td><code> Formatted Date: Tue Jan 1 00:00:00 1985</code></td> </tr> </tbody> </table> </figure> <hr class="wp-block-separator has-alpha-channel-opacity"/> <h2 id="block-323ff1c3-82fc-4193-9c8d-125f5d3c2efb">Summary</h2> <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> <p id="block-7790db1b-2ed9-424b-885a-382987e3d867">Good Luck & Happy Coding!</p> <hr class="wp-block-separator has-alpha-channel-opacity"/> <h2>Programmer Humor – Blockchain</h2> <div class="wp-block-image"> <figure class="aligncenter size-full"><img loading="lazy" 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="(max-width: 280px) 100vw, 280px" /><figcaption><em>“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.”</em> <strong>source </strong> – <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> </div> </div> https://www.sickgaming.net/blog/2022/08/04/how-to-convert-epoch-time-to-date-time-in-python/ |