<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Azqora Tech]]></title><description><![CDATA[Azqora Tech]]></description><link>https://azqoratech.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Wed, 18 Nov 2026 14:58:59 GMT</lastBuildDate><atom:link href="https://azqoratech.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Unix timestamps in JavaScript: seconds vs milliseconds]]></title><description><![CDATA[A Unix timestamp counts the time since 1 January 1970 00:00:00 UTC. The most common bug is mixing up the unit. Many APIs and databases use seconds, while…]]></description><link>https://azqoratech.hashnode.dev/unix-timestamps-in-javascript-seconds-vs-milliseconds</link><guid isPermaLink="true">https://azqoratech.hashnode.dev/unix-timestamps-in-javascript-seconds-vs-milliseconds</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[webdev]]></category><dc:creator><![CDATA[Shahabaz Alam]]></dc:creator><pubDate>Tue, 17 Nov 2026 03:52:10 GMT</pubDate><content:encoded><![CDATA[<p>A Unix timestamp counts the time since 1 January 1970 00:00:00 UTC. The most common bug is mixing up the unit. Many APIs and databases use <strong>seconds</strong>, while JavaScript uses <strong>milliseconds</strong>. The result is a date that is either in January 1970 or tens of thousands of years in the future.</p>
<h2>The quick way to tell them apart</h2>
<p>For dates around today:</p>
<ul>
<li>A <strong>10-digit</strong> number such as <code>1700000000</code> is in seconds.</li>
<li>A <strong>13-digit</strong> number such as <code>1700000000000</code> is in milliseconds.</li>
</ul>
<h2>What JavaScript expects</h2>
<pre><code class="language-js">// Date works in milliseconds
Date.now();                 // for example 1700000000000
new Date(1700000000000);    // correct

// A seconds value must be multiplied by 1000
const seconds = 1700000000;
new Date(seconds * 1000);   // correct
new Date(seconds);          // wrong: shows January 1970
</code></pre>
<h2>Getting seconds from a Date</h2>
<pre><code class="language-js">const nowSeconds = Math.floor(Date.now() / 1000);
</code></pre>
<p>Use <code>Math.floor</code>, not <code>Math.round</code>, so the value never jumps one second ahead of the real time.</p>
<h2>A small helper that accepts both</h2>
<pre><code class="language-js">function toDate(ts) {
  const n = Number(ts);
  // Below this value it is almost certainly seconds
  return new Date(n &lt; 1e11 ? n * 1000 : n);
}
</code></pre>
<p>This is a guess based on size, so use it only for display or debugging. In production code, store the unit in the field name, for example <code>created_at_ms</code>.</p>
<h2>Other things that go wrong</h2>
<ul>
<li><strong>Time zones:</strong> a timestamp has no time zone. The zone only appears when you format it. Show <code>toISOString()</code> in UTC when you debug, to remove doubt.</li>
<li><strong>Strings instead of numbers:</strong> JSON from some APIs sends the timestamp as a string. Convert it with <code>Number()</code> first.</li>
<li><strong>Precision:</strong> a timestamp in nanoseconds is too large for a normal JavaScript number to hold exactly. Use <code>BigInt</code> for it.</li>
<li><strong>The year 2038:</strong> a 32-bit signed integer holds seconds only until January 2038. Use 64-bit fields in databases.</li>
</ul>
<h2>Checklist</h2>
<ol>
<li>Count the digits before you convert.</li>
<li>Name the unit in the field name.</li>
<li>Compare the result against a date you already know.</li>
<li>Debug in UTC, then format for the user.</li>
</ol>
<p>Disclosure: I work on SnipFerry, a set of free browser-based JSON, CSV and SQL tools (<a href="https://snipferry.com">https://snipferry.com</a>). The examples above are plain JavaScript and do not depend on any tool.</p>
]]></content:encoded></item></channel></rss>