snipt

Ctrl+h for KB shortcuts

JavaScript

Convert epoch time to a human-readable date in JavaScript

// We create an array of month names to lookup later.
var monthNames = ["January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"
];

// Get the data.start_time, which is in seconds, and multiply by 1000 to get into milliseconds. We gove the milliseconds number as input to the Date constructor function. We end up have an object/variable called session_date that is of type Date.
var session_date = new Date(1000*1476220872);

// We create the string that we want to append to the DOM. For month, we need to get the month name from the array we created above to map month numbers to month names.
var session_date_string = monthNames[session_date.getMonth()] + " " + session_date.getDay() + ", " + session_date.getFullYear()

// Log to console to make sure it looks as expected.
console.log(session_date_string)

// Append to the DOM with jQuery or rewrite to use vanialla JavaScript if preferred.
$('session-date').append(session_date_string)

Description

Convert epoch time to a human-readable date in JavaScript
https://snipt.net/embed/26663f6490e0f3f3c394cd42e45ecf4e/
/raw/26663f6490e0f3f3c394cd42e45ecf4e/
26663f6490e0f3f3c394cd42e45ecf4e
js
JavaScript
16
2019-06-18T12:49:44
True
False
False
Oct 11, 2016 at 05:34 PM
/api/public/snipt/147987/
convert-epoch-time-to-a-human-readable-date-in-javascript
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1"> 1</a> <a href="#L-2"> 2</a> <a href="#L-3"> 3</a> <a href="#L-4"> 4</a> <a href="#L-5"> 5</a> <a href="#L-6"> 6</a> <a href="#L-7"> 7</a> <a href="#L-8"> 8</a> <a href="#L-9"> 9</a> <a href="#L-10">10</a> <a href="#L-11">11</a> <a href="#L-12">12</a> <a href="#L-13">13</a> <a href="#L-14">14</a> <a href="#L-15">15</a> <a href="#L-16">16</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="c1">// We create an array of month names to lookup later.</span> </span><span id="L-2"><a name="L-2"></a><span class="kd">var</span> <span class="nx">monthNames</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;January&quot;</span><span class="p">,</span> <span class="s2">&quot;February&quot;</span><span class="p">,</span> <span class="s2">&quot;March&quot;</span><span class="p">,</span> <span class="s2">&quot;April&quot;</span><span class="p">,</span> <span class="s2">&quot;May&quot;</span><span class="p">,</span> <span class="s2">&quot;June&quot;</span><span class="p">,</span> </span><span id="L-3"><a name="L-3"></a> <span class="s2">&quot;July&quot;</span><span class="p">,</span> <span class="s2">&quot;August&quot;</span><span class="p">,</span> <span class="s2">&quot;September&quot;</span><span class="p">,</span> <span class="s2">&quot;October&quot;</span><span class="p">,</span> <span class="s2">&quot;November&quot;</span><span class="p">,</span> <span class="s2">&quot;December&quot;</span> </span><span id="L-4"><a name="L-4"></a><span class="p">];</span> </span><span id="L-5"><a name="L-5"></a> </span><span id="L-6"><a name="L-6"></a><span class="c1">// Get the data.start_time, which is in seconds, and multiply by 1000 to get into milliseconds. We gove the milliseconds number as input to the Date constructor function. We end up have an object/variable called session_date that is of type Date.</span> </span><span id="L-7"><a name="L-7"></a><span class="kd">var</span> <span class="nx">session_date</span> <span class="o">=</span> <span class="k">new</span> <span class="nb">Date</span><span class="p">(</span><span class="mi">1000</span><span class="o">*</span><span class="mi">1476220872</span><span class="p">);</span> </span><span id="L-8"><a name="L-8"></a> </span><span id="L-9"><a name="L-9"></a><span class="c1">// We create the string that we want to append to the DOM. For month, we need to get the month name from the array we created above to map month numbers to month names.</span> </span><span id="L-10"><a name="L-10"></a><span class="kd">var</span> <span class="nx">session_date_string</span> <span class="o">=</span> <span class="nx">monthNames</span><span class="p">[</span><span class="nx">session_date</span><span class="p">.</span><span class="nx">getMonth</span><span class="p">()]</span> <span class="o">+</span> <span class="s2">&quot; &quot;</span> <span class="o">+</span> <span class="nx">session_date</span><span class="p">.</span><span class="nx">getDay</span><span class="p">()</span> <span class="o">+</span> <span class="s2">&quot;, &quot;</span> <span class="o">+</span> <span class="nx">session_date</span><span class="p">.</span><span class="nx">getFullYear</span><span class="p">()</span> </span><span id="L-11"><a name="L-11"></a> </span><span id="L-12"><a name="L-12"></a><span class="c1">// Log to console to make sure it looks as expected.</span> </span><span id="L-13"><a name="L-13"></a><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">session_date_string</span><span class="p">)</span> </span><span id="L-14"><a name="L-14"></a> </span><span id="L-15"><a name="L-15"></a><span class="c1">// Append to the DOM with jQuery or rewrite to use vanialla JavaScript if preferred.</span> </span><span id="L-16"><a name="L-16"></a><span class="nx">$</span><span class="p">(</span><span class="s1">&#39;session-date&#39;</span><span class="p">).</span><span class="nx">append</span><span class="p">(</span><span class="nx">session_date_string</span><span class="p">)</span> </span></pre></div> </td></tr></table>
date, time
--- 
+++ 
@@ -0,0 +1,16 @@
+// We create an array of month names to lookup later.
+var monthNames = ["January", "February", "March", "April", "May", "June",
+  "July", "August", "September", "October", "November", "December"
+];
+
+// Get the data.start_time, which is in seconds, and multiply by 1000 to get into milliseconds. We gove the milliseconds number as input to the Date constructor function. We end up have an object/variable called session_date that is of type Date.
+var session_date = new Date(1000*1476220872);
+
+// We create the string that we want to append to the DOM. For month, we need to get the month name from the array we created above to map month numbers to month names.
+var session_date_string = monthNames[session_date.getMonth()] + " " + session_date.getDay() + ", " + session_date.getFullYear()
+
+// Log to console to make sure it looks as expected.
+console.log(session_date_string)
+
+// Append to the DOM with jQuery or rewrite to use vanialla JavaScript if preferred.
+$('session-date').append(session_date_string)