Emmanuel Akinfulubi

JavaScriptDate

Unix timestamp in seconds vs milliseconds in JavaScript

Intermediate · 4 min read

You fetch a user from an API and their "created" date shows as January 1970. Nothing is broken in the data. The API sent a Unix timestamp in seconds, and JavaScript read it as milliseconds. This mix-up is one of the most common bugs when JavaScript talks to servers, databases and other languages.

In this tutorial you will write two small helpers: toUnixSeconds(date) and fromUnixSeconds(seconds). You will see exactly what the 1970 bug looks like, so you can spot it quickly, and why rounding down with Math.floor is the right choice. You need to know how to make a date, and that getTime() gives a date's timestamp.

Two ways to count from 1970

A timestamp is a single number for a moment in time: how much time has passed since midnight UTC on 1 January 1970, a starting point called the Unix epoch.

  • JavaScript counts that time in milliseconds. getTime() and Date.now() give milliseconds.
  • Most other systems count in seconds: Unix tools, many databases, many APIs and languages like Python and PHP. That count is called Unix time.

The difference is a factor of 1000. Divide by 1000 going out of JavaScript, and multiply by 1000 coming in.

toISOString for checking

To see a moment without your time zone getting in the way, toISOString() prints it in UTC. The Z at the end means UTC:

JavaScript

console.log(new Date(0).toISOString()); // '1970-01-01T00:00:00.000Z'

A timestamp of 0 is the epoch itself.

Here is the exercise. Have a go yourself first if you like, then read on and we will build it step by step.

38 Unix seconds and JavaScript milliseconds

fair

Convert a date to a Unix timestamp in seconds and back. Then show what happens when a seconds value is handed to new Date by mistake.

Stuck? Three hints, smallest first
  1. 1. JavaScript counts milliseconds; almost every other system counts seconds. The factor is 1000.
  2. 2. Divide and floor going out, multiply coming back in.
  3. 3. A seconds value read as milliseconds gives a date in 1970, which is the classic symptom.

Step 1: date to Unix seconds

Take the milliseconds, divide by 1000, and round down:

Step 1

const toUnixSeconds = (date) => Math.floor(date.getTime() / 1000);
const instant = new Date(Date.UTC(2026, 0, 15));
console.log(instant.getTime());      // 1768435200000
console.log(toUnixSeconds(instant)); // 1768435200

Date.UTC(2026, 0, 15) is midnight UTC on 15 January 2026. Its millisecond count ends in three zeros. Unix seconds drop them.

Step 2: Unix seconds back to a date

Multiply by 1000 and hand the result to new Date:

Step 2

const fromUnixSeconds = (seconds) => new Date(seconds * 1000);
console.log(fromUnixSeconds(1768435200).toISOString()); // '2026-01-15T00:00:00.000Z'
console.log(toUnixSeconds(fromUnixSeconds(1768435200))); // 1768435200

The round trip brings you back to the same number.

Step 3: the wrong turn, forgetting the 1000

Now make the mistake on purpose. Pass a seconds value straight to new Date:

Step 3

const asSeconds = new Date(1000000000);
const asMilliseconds = new Date(1000000000 * 1000);
console.log(asSeconds.toISOString());      // '1970-01-12T13:46:40.000Z'
console.log(asMilliseconds.toISOString()); // '2001-09-09T01:46:40.000Z'

The number 1,000,000,000 is a moment in September 2001 when read as seconds. Read as milliseconds, it is only about eleven and a half days after the epoch, in January 1970. That is the symptom to remember: a date in January 1970 almost always means a seconds value that was never multiplied by 1000.

The opposite mistake happens too. If you send JavaScript's milliseconds to a system that expects seconds, the number is 1000 times too big, and that system sees a date tens of thousands of years in the future.

Step 4: sub-second detail is dropped

Unix seconds are whole seconds, so anything smaller is thrown away:

Step 4

console.log(toUnixSeconds(new Date(Date.UTC(2026, 0, 15, 0, 0, 0, 999)))); // 1768435200

999 milliseconds past midnight still gives the same second. If you need milliseconds, keep the JavaScript timestamp instead.

Step 5: why Math.floor, not Math.trunc

For dates after 1970, Math.floor and Math.trunc give the same result. They differ for dates before 1970, where the timestamp is negative:

Step 5

console.log(toUnixSeconds(new Date(-1500))); // -2
console.log(Math.trunc(-1500 / 1000));      // -1

-1500 milliseconds is one and a half seconds before the epoch. Math.floor rounds down, to -2. Math.trunc just chops off the decimals, which moves -1.5 toward zero, to -1. Unix time counts whole seconds rounded down, so floor keeps older dates correct.

The full solution

The panel has both helpers. It prints 15 January 2026 as Unix seconds and back, then shows the same number read as milliseconds and as seconds. The checks cover the round trip, the dropped milliseconds, the 1970 symptom and the floor versus trunc case. Press Run to see them.

The answer

const toUnixSeconds = (date) => Math.floor(date.getTime() / 1000);
const fromUnixSeconds = (seconds) => new Date(seconds * 1000);

const instant = new Date(Date.UTC(2026, 0, 15));

// The mistake: a seconds value passed straight in.
const asSeconds = new Date(1000000000);          // read as milliseconds -> 1970
const asMilliseconds = new Date(1000000000 * 1000);

console.log('date -> seconds:', toUnixSeconds(instant));
console.log('seconds -> date:', fromUnixSeconds(1768435200).toISOString());
console.log('1000000000 as ms:', asSeconds.toISOString(), '<- eleven days after the epoch');
console.log('1000000000 as s :', asMilliseconds.toISOString(), '<- September 2001');

Output


      
9 checks that prove this answer

You do not have to read these. They are part of the answer panel above, so pressing Run there executes all 9 of them and prints ok for each. That output is the proof, and the build fails if a single check fails.

9 checks

test('15 January 2026 UTC in Unix seconds', toUnixSeconds(instant), 1768435200);
test('and back again', fromUnixSeconds(1768435200).toISOString(), '2026-01-15T00:00:00.000Z');
test('the round trip is lossless for a whole second', toUnixSeconds(fromUnixSeconds(1768435200)), 1768435200);
test('but sub-second precision is discarded', toUnixSeconds(new Date(Date.UTC(2026, 0, 15, 0, 0, 0, 999))), 1768435200);
test('a seconds value read as milliseconds lands in 1970', asSeconds.getUTCFullYear(), 1970);
test('while the same number times 1000 is 2001', asMilliseconds.getUTCFullYear(), 2001);
test('so a "date" in 1970 usually means a missing multiplication', asSeconds.getUTCMonth(), 0);
test('floor, not trunc: before 1970 they differ', toUnixSeconds(new Date(-1500)), -2);
test('whereas Math.trunc would give -1 and lose half a second', Math.trunc(-1500 / 1000), -1);

The factor of 1000 is the single most common integration bug between JavaScript and anything else. A date that renders as 1 January 1970 or as a day in mid-January 1970 is almost always a seconds value that was never multiplied.

Math.floor rather than Math.trunc matters only for dates before 1970, where the timestamp is negative — and there the two disagree by a second, with trunc rounding towards the epoch. Unix seconds are defined as the floor, so use floor and the pre-1970 cases stay correct.

Common mistakes

  • Passing seconds to new Date. You get a date in January 1970. Multiply by 1000 first.
  • Sending milliseconds to an API that wants seconds. Divide by 1000 and round down.
  • Rounding with Math.round or Math.trunc. Use Math.floor so the result is always the second the moment falls in.

Try it yourself

  1. Write nowInUnixSeconds() using Date.now().
  2. Write looksLikeSeconds(n) that guesses whether a timestamp is in seconds, by checking whether it is less than 100,000,000,000. Test it on both numbers from Step 3.
  3. Turn the Unix time 1700000000 into a date and print it with toISOString().

Keep practising

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.