Emmanuel Akinfulubi

JavaScriptDate

Create a date from milliseconds in JavaScript

Beginner · 4 min read

Servers, databases and APIs often send times as one big number, such as 1768435200000. That number is a timestamp. In this tutorial you will turn timestamps into JavaScript dates, try a few special ones (the starting point, one day after it, one day before it), and turn a date back into its number.

You will also see why the same timestamp can show a different calendar day depending on where you are, and how to read it so that everyone agrees. You need to know how to make a date and use console.log.

What a timestamp is

JavaScript measures time as the number of milliseconds since one fixed moment: midnight UTC at the start of 1 January 1970. That moment is called the epoch. UTC is the world's reference clock, the time at Greenwich with no daylight saving.

  • Timestamp 0 is the epoch itself.
  • Positive numbers are after it.
  • Negative numbers are before it.

A millisecond is a thousandth of a second, so a day is a big number:

JavaScript

const MS_PER_DAY = 24 * 60 * 60 * 1000;
console.log(MS_PER_DAY); // 86400000

One number, one moment

A timestamp is an instant: a single moment that is the same everywhere in the world. Calendar dates are not like that. At that one instant it might be Thursday evening in New York and already Friday in Lagos. Keep this difference in mind; it explains Step 4.

Here is the exercise. Try it yourself first if you like, then read on and we will build it together.

15 A date from a timestamp

easy

Build dates from millisecond timestamps: the epoch, one day after it, and one day before. Then round-trip a date back to its number.

Stuck? Three hints, smallest first
  1. 1. One argument that is a number is a millisecond count from 1 January 1970 UTC.
  2. 2. A day is 86,400,000 milliseconds.
  3. 3. Negative numbers are allowed, and mean before 1970.

Step 1: a date from a number

Pass one number to new Date, and it is read as a timestamp:

Step 1

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

toISOString() shows the moment in UTC. The Z at the end stands for UTC. getTime() hands back the number you started with.

Step 2: one day either side

Add or subtract one day's worth of milliseconds:

Step 2

const dayAfter = new Date(MS_PER_DAY);
const dayBefore = new Date(-MS_PER_DAY);
console.log(dayAfter.toISOString());  // '1970-01-02T00:00:00.000Z'
console.log(dayBefore.toISOString()); // '1969-12-31T00:00:00.000Z'

Negative timestamps are fine. They are how JavaScript stores any date before 1970, such as a birthday in 1965.

Step 3: a round trip

Take a real timestamp, make a date from it, and read the number back:

Step 3

const stamp = 1768435200000;
console.log(new Date(stamp).getTime() === stamp); // true
console.log(new Date(stamp).toISOString());       // '2026-01-15T00:00:00.000Z'
console.log(Date.UTC(2026, 0, 15));                // 1768435200000

The number comes back exactly. That one is midnight UTC on 15 January 2026. Date.UTC(year, month, day) works the other way: it builds the timestamp from UTC parts. Months count from 0 here too, so 0 is January.

Step 4: the local reading changes by zone

toISOString() always reads in UTC. The local getters, like getFullYear(), read in the time zone of the computer running the code:

Step 4

console.log(epoch.getFullYear()); // 1970 in Lagos; 1969 in New York

In Lagos, one hour ahead of UTC, the epoch is 1:00 AM on 1 January 1970. In New York, five hours behind, the same instant is 7:00 PM on 31 December 1969. Same number, different calendar year.

So when you want to show or test a timestamp in a way that is the same for everyone, use toISOString() or getTime(). The answer on the page does exactly that. Its only check on a local getter accepts either 1969 or 1970, because both are correct somewhere.

A warning about using 0 for "no date"

new Date(0) is a handy example while learning. Don't use it to mean "no date set" in real code. It is a perfectly real moment in 1970, so nothing later can tell it apart from a genuine date. Use null for "no date".

The full solution

The panel builds the epoch and the days either side, prints them in UTC, and prints the epoch's local year, which depends on your zone. Press Run to see it.

The answer

const MS_PER_DAY = 86400000;

const epoch = new Date(0);
const dayAfter = new Date(MS_PER_DAY);
const dayBefore = new Date(-MS_PER_DAY);

// A timestamp is an INSTANT, not a calendar date. toISOString shows it in UTC,
// which is the only reading that is the same everywhere.
console.log('0             ->', epoch.toISOString());
console.log('86400000      ->', dayAfter.toISOString());
console.log('-86400000     ->', dayBefore.toISOString());
console.log('local reading of 0:', epoch.getFullYear(), '<- 1969 west of Greenwich');

Output


      
8 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 8 of them and prints ok for each. That output is the proof, and the build fails if a single check fails.

8 checks

test('zero is the epoch', epoch.toISOString(), '1970-01-01T00:00:00.000Z');
test('and its timestamp is zero', epoch.getTime(), 0);
test('a day later', dayAfter.toISOString(), '1970-01-02T00:00:00.000Z');
test('a day earlier, using a negative number', dayBefore.toISOString(), '1969-12-31T00:00:00.000Z');
test('a timestamp round-trips exactly', new Date(1768435200000).getTime(), 1768435200000);
test('and that one is 15 January 2026 in UTC', new Date(1768435200000).toISOString(), '2026-01-15T00:00:00.000Z');
test('Date.UTC computes the same number', Date.UTC(2026, 0, 15), 1768435200000);
test('the local calendar year of the epoch is 1969 or 1970, depending on the zone', [1969, 1970].includes(epoch.getFullYear()), true);

One number, one instant. The last assertion is the honest one: the epoch's local year is 1970 in Lagos and 1969 in New York, because local midnight UTC is still the previous evening there. That is why every assertion about a timestamp in this section is written against toISOString or getTime, never against a local getter.

new Date(0) is also a handy sentinel while learning, but do not use it as a "no date" value in real code: it is a perfectly valid instant, so nothing downstream can tell it from a real one.

Common mistakes

  • Treating a timestamp as seconds. JavaScript uses milliseconds. A seconds value such as 1768435200 lands in January 1970.
  • Checking a timestamp with local getters. getFullYear() or getDate() can differ by zone. Use toISOString() or getTime().
  • Using new Date(0) as an empty value. It is a real date in 1970, so it hides the fact that nothing was set.

Try it yourself

  1. Build the date one week after the epoch and print it with toISOString().
  2. A server sends 1768435200 in seconds. Turn it into a date correctly.
  3. Find the timestamp of your own birthday with Date.UTC. Is it negative?

Keep practising

Newsletter

New writing, when it lands.

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