Emmanuel Akinfulubi

JavaScriptDate

Date.UTC and UTC getters in JavaScript

Intermediate · 4 min read

A flight leaves at 09:30 UTC. A server logs an error at 14:05 UTC. Much of the time data you deal with is given in UTC, the world's reference clock, rather than in your own local time. In this tutorial you will build a date that means exactly 09:30 UTC on 15 January 2026, read its parts back in UTC, and then see how those parts relate to the local time on your own computer.

You will also meet getTimezoneOffset(), whose sign surprises almost everyone the first time. You need to know how to make a date with new Date(year, month, day) and that months count from 0.

Local time and UTC

Every JavaScript date is one exact moment, stored as milliseconds since the start of 1970 in UTC. You can read that moment in two ways:

  • Local time is the clock of the time zone your computer is set to.
  • UTC is the time at Greenwich, with no daylight saving. It is the same for everyone.

Lagos is one hour ahead of UTC. So at 09:30 UTC, a clock in Lagos shows 10:30.

new Date(2026, 0, 15, 9, 30) means 09:30 local time. To mean 09:30 UTC, you need something else.

Date.UTC returns a number

Date.UTC takes the same parts as new Date (year, month, day, hours, minutes), with the month still counted from 0. But it reads them as UTC, and it gives back a number, the timestamp, not a date object:

JavaScript

const stamp = Date.UTC(2026, 0, 15, 9, 30);
console.log(stamp);        // 1768469400000
console.log(typeof stamp); // 'number'

To get a date you can call methods on, pass that number to new Date.

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

16 Date.UTC and the UTC getters

fair

Build a date that means 09:30 UTC on 15 January 2026, and read it back with the UTC getters. Then show how the local getters relate to them.

Stuck? Three hints, smallest first
  1. 1. Date.UTC(...) takes the same arguments as the constructor but returns a number.
  2. 2. Every local getter has a UTC twin: getUTCDate, getUTCHours, getUTCDay.
  3. 3. getTimezoneOffset() is minutes of UTC minus local, so local minutes are UTC minutes minus the offset.

Step 1: build the UTC date

Step 1

const utc = new Date(stamp);
console.log(utc.toISOString()); // '2026-01-15T09:30:00.000Z'

toISOString() always shows UTC, and the Z at the end means UTC. It shows exactly what you asked for.

Step 2: read the parts in UTC

Every local getter has a UTC twin with UTC in its name: getUTCFullYear, getUTCMonth, getUTCDate, getUTCDay, getUTCHours, getUTCMinutes and so on.

Step 2

console.log(utc.getUTCFullYear(), utc.getUTCMonth(), utc.getUTCDate()); // 2026 0 15
console.log(utc.getUTCHours(), utc.getUTCMinutes()); // 9 30
console.log(utc.getUTCDay()); // 4

Month 0 is January, and day of the week 4 is Thursday. These answers are the same on every computer in the world, because UTC doesn't depend on where you are.

Step 3: the local getters may differ

The ordinary getters read local time:

Step 3

console.log(utc.getHours(), utc.getMinutes()); // 10 30 in Lagos

In Lagos the hour is 10. In London in January it would be 9, and in New York 4. Same moment, different clock faces.

Step 4: getTimezoneOffset and its sign

getTimezoneOffset() tells you the gap between UTC and local time, in minutes. The sign is the surprise. It is UTC minus local, not local minus UTC:

Step 4

console.log(utc.getTimezoneOffset()); // -60 in Lagos

Lagos is one hour ahead of UTC, and the offset is -60. A zone behind UTC, such as New York in winter, gives a positive number, 300. If you remember only one thing from this lesson, remember the sign.

Step 5: the link between the two

If the offset is "UTC minus local", then local is UTC minus the offset. You can check that with minutes since midnight:

Step 5

const offset = utc.getTimezoneOffset();
const utcMinutes = utc.getUTCHours() * 60 + utc.getUTCMinutes();
const localMinutes = utc.getHours() * 60 + utc.getMinutes();
console.log((utcMinutes - offset + 2880) % 1440 === localMinutes); // true

There are 1,440 minutes in a day. Taking % 1440 wraps the result around midnight, for zones where local time is already tomorrow or still yesterday. Adding 2,880 (two days of minutes) first keeps the number positive before the %, since % on a negative number gives a negative answer in JavaScript.

This line is true in every time zone. That is how the answer on the page checks time zone behavior without assuming where the reader is.

The full solution

The panel builds the UTC date, prints its UTC parts and your machine's offset, and checks the relationship from Step 5. Press Run and look at the line that reports your offset.

The answer

// Date.UTC returns a timestamp, not a Date, so it has to be wrapped.
const stamp = Date.UTC(2026, 0, 15, 9, 30);      // month still zero-based
const utc = new Date(stamp);

// The offset is minutes of UTC minus local: -60 for a zone an hour ahead.
const offset = utc.getTimezoneOffset();
const utcMinutes = utc.getUTCHours() * 60 + utc.getUTCMinutes();
const localMinutes = utc.getHours() * 60 + utc.getMinutes();

console.log('ISO      :', utc.toISOString());
console.log('UTC parts:', utc.getUTCFullYear(), utc.getUTCMonth(), utc.getUTCDate(), utc.getUTCHours() + ':' + utc.getUTCMinutes());
console.log('this machine is', -offset, 'minutes from UTC, so locally it reads', utc.getHours() + ':' + utc.getMinutes());

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('Date.UTC hands back a number', typeof stamp, 'number');
test('the UTC year and month', [utc.getUTCFullYear(), utc.getUTCMonth()], [2026, 0]);
test('the UTC day of the month', utc.getUTCDate(), 15);
test('the UTC time', [utc.getUTCHours(), utc.getUTCMinutes()], [9, 30]);
test('15 January 2026 is a Thursday in UTC too', utc.getUTCDay(), 4);
test('the ISO form is exactly what was asked for', utc.toISOString(), '2026-01-15T09:30:00.000Z');
test('local time is UTC minus the offset, whatever zone this runs in', (utcMinutes - offset + 2880) % 1440, localMinutes);
test('and the offset is a whole number of minutes', offset % 1, 0);

Date.UTC is the counterpart of the multi-argument constructor: same arguments, same zero-based month, but it interprets them as UTC and returns a timestamp rather than an object. Use it whenever the parts you hold are already UTC, and use it inside date arithmetic to sidestep daylight saving.

The offset assertion is the one worth reading twice. getTimezoneOffset() returns UTC minus local, so a zone one hour ahead of UTC reports -60. The sign surprises everyone once. Writing the relationship as an identity is also how you assert something about time zones without asserting anything about the reader's machine.

Common mistakes

  • Treating Date.UTC as a date. It returns a number. Wrap it: new Date(Date.UTC(...)).
  • Reading a UTC date with local getters. getHours() gives local time. Use getUTCHours() when you want the UTC answer.
  • Getting the offset backwards. A zone ahead of UTC has a negative offset. Lagos is -60, not 60.

Try it yourself

  1. Build 23:30 UTC on 31 December 2026. In Lagos, what are its local year, month and day?
  2. Write utcHHMM(date) that returns text like 09:30 from the UTC getters.
  3. Turn the offset into a label such as UTC+1. (Hint: flip the sign and divide by 60.)

Keep practising

Newsletter

New writing, when it lands.

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