Date string parsing differences in JavaScript
Intermediate · 4 min read
'2026-01-15', '2026/01/15', '2026-01-15T00:00:00' and '2026-01-15T00:00:00Z' all look like the same date. Pass them to new Date, though, and they don't all give the same moment. In this tutorial you will parse all four (and one with +01:00 on the end), and find out which are read as UTC, which as local time, and why.
By the end you will know which form to send between a browser and a server so that this never causes a bug. You need to know how to make a date, and the difference between local time (your computer's time zone) and UTC (the world's reference clock).
Two kinds of text
The JavaScript standard describes one family of date text exactly: the ISO forms, such as 2026-01-15 and 2026-01-15T09:30:00. For those, the rules are fixed:
- Date only is read as midnight UTC.
- Date and time, no zone is read as local time.
- Date and time with a zone, such as
Z(meaning UTC) or+01:00, is read in that zone.
Anything else, such as slashes or 'January 15, 2026', isn't covered by those rules. Each JavaScript engine reads it with its own older code, called a legacy parser. Engines have disagreed about these forms in the past, so they aren't safe to rely on.
Comparing moments
Two dates are the same moment when their getTime() numbers match. Date.UTC(year, month, day, ...) gives the number for a UTC moment, which makes a handy yardstick. Months count from 0, so January is 0.
Here is the exercise. Try it first if you like, then read on and we will build it together.
18 Dashes, slashes and the Z suffix
fairCompare how '2026-01-15', '2026/01/15', '2026-01-15T00:00:00' and '2026-01-15T00:00:00Z' are parsed.
Stuck? Three hints, smallest first
- 1. Only the ISO forms are described by the specification. Slashes go to the engine’s legacy parser.
- 2. The legacy parser treats what it reads as local time.
- 3. An explicit
Zor+01:00removes all doubt, which is why an API should always send one.
Step 1: the two UTC forms
const bare = new Date('2026-01-15');
const zulu = new Date('2026-01-15T00:00:00Z');
console.log(bare.toISOString()); // '2026-01-15T00:00:00.000Z'
console.log(zulu.getTime() === Date.UTC(2026, 0, 15)); // true
console.log(bare.getTime() === zulu.getTime()); // true
The Z says UTC outright. The bare date-only form is UTC by the rule above. So they are the same moment, in every time zone.
Step 2: the local ISO form
const localIso = new Date('2026-01-15T00:00:00');
console.log(localIso.getDate(), localIso.getHours()); // 15 0
console.log(localIso.toISOString()); // '2026-01-14T23:00:00.000Z' in Lagos
With a time and no zone, the text means local midnight. In Lagos, one hour ahead of UTC, local midnight is 23:00 UTC the evening before, which is what toISOString() shows. On a computer set to UTC, the two would match.
Step 3: slashes
const slashes = new Date('2026/01/15');
console.log(slashes.getDate(), slashes.getMonth()); // 15 0
console.log(slashes.getTime() === localIso.getTime()); // true
Slashes go to the legacy parser. In node it reads them as local time, and the checks on this page confirm the same in your browser. So '2026/01/15' matches the local ISO form, not the bare date with dashes. Swapping a dash for a slash moves the moment by your whole time zone offset.
Step 4: the gap is the offset
How far apart are the local and UTC readings? Exactly the time zone offset:
const offset = slashes.getTimezoneOffset();
console.log(offset); // -60 in Lagos
console.log(slashes.getTime() - bare.getTime() === offset * 60000); // true
getTimezoneOffset() gives UTC minus local, in minutes, so Lagos is -60. Multiply by 60,000 to turn minutes into milliseconds. In Lagos the gap is one hour; on a server set to UTC it is zero. That is why code that only ever runs on a UTC server can hide this bug for years.
Step 5: an explicit offset
You can also write the zone as a number of hours:
const plusOne = new Date('2026-01-15T00:00:00+01:00');
console.log(plusOne.toISOString()); // '2026-01-14T23:00:00.000Z'
console.log(plusOne.getTime() === Date.UTC(2026, 0, 14, 23, 0)); // true
Midnight at +01:00 is 23:00 UTC the day before. Unlike Step 2, this answer is the same on every computer, because the text says which zone it means.
What to send
For an exact moment, send ISO text with a zone: 2026-01-15T09:30:00Z or 2026-01-15T10:30:00+01:00. It means one thing everywhere. Avoid slashes, and don't send a time without a zone. A check like Number.isNaN(date.getTime()) will tell you if a string couldn't be read at all; all five here are valid.
The full solution
The panel parses all five strings, prints them in UTC, and checks each rule above. The lines for the slash and local ISO forms change with your time zone. Press Run to see your values.
const bare = new Date('2026-01-15'); // ISO, date only -> UTC
const slashes = new Date('2026/01/15'); // legacy parser -> local
const localIso = new Date('2026-01-15T00:00:00'); // ISO, no offset -> local
const zulu = new Date('2026-01-15T00:00:00Z'); // ISO with Z -> UTC
const plusOne = new Date('2026-01-15T00:00:00+01:00');
// The gap between a local reading and a UTC reading IS the zone offset.
const offset = slashes.getTimezoneOffset();
console.log('bare :', bare.toISOString());
console.log('slashes :', slashes.toISOString(), '<- moves with the machine zone');
console.log('localIso :', localIso.toISOString(), '<- same as slashes');
console.log('zulu :', zulu.toISOString());
console.log('+01:00 :', plusOne.toISOString());
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.
test('slashes are parsed as local, exactly like the ISO form with a time', slashes.getTime(), localIso.getTime());
test('so their local day is the 15th', slashes.getDate(), 15);
test('and their local month is January', slashes.getMonth(), 0);
test('the Z suffix means UTC', zulu.getTime(), Date.UTC(2026, 0, 15));
test('so the bare date-only string matches it', bare.getTime(), zulu.getTime());
test('the local and UTC readings differ by exactly the zone offset', slashes.getTime() - bare.getTime(), offset * 60000);
test('an explicit +01:00 is an hour before UTC midnight', plusOne.getTime(), Date.UTC(2026, 0, 14, 23, 0));
test('none of the four is invalid', [bare, slashes, localIso, zulu].every((d) => !Number.isNaN(d.getTime())), true);
Two of these forms are in the specification and two are not. YYYY-MM-DD and YYYY-MM-DDTHH:mm:ss are defined, the first as UTC and the second as local. '2026/01/15' and 'January 15, 2026' fall through to each engine's own parser, which historically differed — that is why new Date('a string I found') is not a portable operation.
The sixth assertion is the useful one to hold on to: the difference between the local reading and the UTC reading is precisely getTimezoneOffset(), which is why it comes out as zero on a server running in UTC and as an hour in Lagos. Send ISO with an explicit offset and none of this arises.
Common mistakes
- Thinking dashes and slashes are the same.
'2026-01-15'is UTC;'2026/01/15'is local. - Sending a time without a zone.
'2026-01-15T09:30:00'means a different moment on every computer that reads it. - Parsing free text like
'January 15, 2026'. It isn't covered by the standard's ISO rules. Parse the parts yourself or use ISO.
Try it yourself
- Parse
'2026-01-15T09:30:00-05:00'and print it withtoISOString(). What hour do you get? - Run the five lines with
TZ=UTC node file.js. Which results change? - Write
sameMoment(a, b)that compares two dates withgetTime(), and test it onbareandslashes.