Parse an ISO date string as local time in JavaScript
Advanced · 4 min read
Dates arrive as text all the time: from forms, from URLs like ?from=2026-01-15, from spreadsheets and from APIs. The ISO format, 2026-01-15 or 2026-01-15T09:30, is the most common. Handing that text straight to new Date has two surprises: a date-only string is read as UTC, not your local time, and impossible dates aren't always refused.
In this tutorial you will write parseLocalIso(text), which reads both formats as local time and returns null for anything malformed or impossible, like 30 February. You need to know how to make a date from parts and that months count from 0. Regular expressions are explained below from the start.
The UTC surprise
UTC is the world's reference clock. Your local time is UTC moved by your time zone. When you write new Date('2026-01-15'), JavaScript treats it as midnight UTC:
console.log(new Date('2026-01-15').getTime() === Date.UTC(2026, 0, 15)); // true
In New York, midnight UTC is still 7 pm the evening before, so new Date('2026-01-15').getDate() is 14 there. Building the date from parts, new Date(2026, 0, 15), gives local midnight instead, which is what most forms mean.
A regular expression in one minute
A regular expression is a pattern for text. A few pieces are enough here:
\dmeans "one digit", and\d{4}means "four digits".^means "the start of the text", and$means "the end".- Round brackets
( )capture a part, so you can read it afterwards. (?: )groups without capturing, and a?after a group makes it optional.
Here is the exercise. Try it on your own first if you like, then read on and we will build it step by step.
55 Parse an ISO date yourself, as local time
hardParse 2026-01-15 and 2026-01-15T09:30 into a local date, rejecting anything malformed or impossible. Do not hand the string to new Date.
Stuck? Three hints, smallest first
- 1. A regular expression with capture groups for the parts, and an optional time section.
- 2. Build with
new Date(y, m - 1, d, ...), which is local, unlike parsing the string. - 3. Then check the built date reads back the parts you put in — that is what rejects 30 February.
Step 1: match a date
const DATE_ONLY = /^(\d{4})-(\d{2})-(\d{2})$/;
console.log(DATE_ONLY.exec('2026-01-15').slice(0, 4)); // [ '2026-01-15', '2026', '01', '15' ]
console.log(DATE_ONLY.exec('15/01/2026')); // null
exec returns an array when the text matches. slice(0, 4) keeps its first four items: item 0 is the whole match, and items 1, 2 and 3 are the captured year, month and day. When the text doesn't match, exec returns null. Because of ^ and $, nothing extra is allowed before or after.
Step 2: add an optional time
const PATTERN = /^(\d{4})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2}))?)?$/;
const withTime = PATTERN.exec('2026-01-15T09:30');
console.log(withTime[4], withTime[5], withTime[6]); // 09 30 undefined
console.log(PATTERN.exec('2026-01-15')[4]); // undefined
After the date there may be a T or a space, then hours and minutes, and optionally : and seconds. [T ] means "a T or a space". When an optional part is missing, its capture is undefined.
Step 3: turn the parts into numbers
const year = Number(withTime[1]);
const month = Number(withTime[2]) - 1;
const day = Number(withTime[3]);
const hours = withTime[4] === undefined ? 0 : Number(withTime[4]);
console.log(year, month, day, hours); // 2026 0 15 9
The captures are strings, so Number converts them. The month needs - 1, because the text says 01 for January but new Date wants 0. A missing time part becomes 0.
Step 4: the wrong turn, trusting the constructor
Now build a local date. But first, see what happens with a date that doesn't exist:
const rolled = new Date(2026, 1, 30); // "30 February 2026"
console.log(rolled.getMonth(), rolled.getDate()); // 2 2
new Date doesn't refuse 30 February. It rolls it into 2 March. So building the date isn't enough to know the input was real.
Step 5: read the parts back
The fix is a round trip. Build the date, then check that it still has the year, month and day you put in. If anything changed, the input was impossible:
function parseLocalIso(text) {
if (typeof text !== 'string') return null;
const match = PATTERN.exec(text);
if (!match) return null;
const year = Number(match[1]);
const month = Number(match[2]) - 1;
const day = Number(match[3]);
const hours = match[4] === undefined ? 0 : Number(match[4]);
const minutes = match[5] === undefined ? 0 : Number(match[5]);
const seconds = match[6] === undefined ? 0 : Number(match[6]);
if (hours > 23 || minutes > 59 || seconds > 59) return null;
const date = new Date(year, month, day, hours, minutes, seconds);
if (date.getFullYear() !== year || date.getMonth() !== month || date.getDate() !== day) {
return null;
}
return date;
}
console.log(parseLocalIso('2026-02-30')); // null
console.log(parseLocalIso('2026-13-01')); // null
console.log(parseLocalIso('2024-02-29').getDate()); // 29
30 February and month 13 are refused. 29 February 2024 is fine, because 2024 is a leap year.
Why check the time separately? new Date(2026, 0, 15, 9, 60) quietly becomes 10:00 on the same day, so the date round trip wouldn't notice. The hours > 23 line also refuses 24:00.
Step 6: local, in every zone
const parsed = parseLocalIso('2026-01-15T09:30');
console.log(parsed.getHours(), parsed.getMinutes()); // 9 30
console.log(parseLocalIso('2026-01-15').getTime() === new Date(2026, 0, 15).getTime()); // true
console.log(parseLocalIso('15/01/2026')); // null
The result equals the date built from parts, so it means the 15th wherever the code runs. Day-first text is refused, because the pattern only accepts ISO order.
The full solution
The panel has PATTERN and parseLocalIso. It prints a date, a date with a time and two refused inputs. The checks cover seconds, 30 February, a leap day, month 13, hour 24 and the gap between this result and new Date(text). Press Run to see them.
// Date, optionally followed by T or a space and a time. Nothing else accepted.
const PATTERN = /^(\d{4})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2}))?)?$/;
function parseLocalIso(text) {
if (typeof text !== 'string') return null;
const match = PATTERN.exec(text);
if (!match) return null;
const year = Number(match[1]);
const month = Number(match[2]) - 1;
const day = Number(match[3]);
const hours = match[4] === undefined ? 0 : Number(match[4]);
const minutes = match[5] === undefined ? 0 : Number(match[5]);
const seconds = match[6] === undefined ? 0 : Number(match[6]);
if (hours > 23 || minutes > 59 || seconds > 59) return null;
const date = new Date(year, month, day, hours, minutes, seconds);
// The constructor rolls impossible dates instead of refusing them, so read the
// parts back: if they changed, the input was not a real date.
if (date.getFullYear() !== year || date.getMonth() !== month || date.getDate() !== day) {
return null;
}
return date;
}
const parsed = parseLocalIso('2026-01-15T09:30');
console.log("'2026-01-15' ->", parseLocalIso('2026-01-15').toDateString());
console.log("'2026-01-15T09:30' ->", parsed.getHours() + ':' + parsed.getMinutes());
console.log("'2026-02-30' ->", parseLocalIso('2026-02-30'), '<- refused');
console.log("'15/01/2026' ->", parseLocalIso('15/01/2026'), '<- refused');
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.
test('a date-only string gives the local calendar date', [parseLocalIso('2026-01-15').getFullYear(), parseLocalIso('2026-01-15').getMonth(), parseLocalIso('2026-01-15').getDate()], [2026, 0, 15]);
test('and it matches a date built from the parts, in any zone', parseLocalIso('2026-01-15').getTime(), new Date(2026, 0, 15).getTime());
test('a time is read too', [parsed.getHours(), parsed.getMinutes(), parsed.getSeconds()], [9, 30, 0]);
test('seconds are optional but accepted', parseLocalIso('2026-01-15T09:30:45').getSeconds(), 45);
test('30 February is refused, not rolled into March', parseLocalIso('2026-02-30'), null);
test('29 February is accepted in a leap year', parseLocalIso('2024-02-29').getDate(), 29);
test('month 13 is refused', parseLocalIso('2026-13-01'), null);
test('a day-first format and an hour of 24 are both refused', [parseLocalIso('15/01/2026'), parseLocalIso('2026-01-15T24:00')], [null, null]);
test('and this differs from new Date by the zone offset', parseLocalIso('2026-01-15').getTime() - new Date('2026-01-15').getTime(), new Date(2026, 0, 15).getTimezoneOffset() * 60000);
Two things are gained by not using the built-in parser. The result is local, so '2026-01-15' means the 15th where the reader is rather than UTC midnight — the last assertion measures exactly that difference. And impossible dates are refused: new Date('2026-02-30') is treated differently by different engines, some giving Invalid Date and some rolling it to 2 March, whereas this returns null everywhere.
The round-trip check is the part to reuse. Since the constructor normalises rather than rejects, the only way to know whether a date was real is to build it and read the parts back out.
Common mistakes
- Passing
'2026-01-15'tonew Date. It is midnight UTC, which is the day before in zones west of UTC. Build from parts. - Forgetting the
- 1on the month.'2026-01-15'would become 15 February. - Trusting the constructor to refuse bad dates. It rolls them over instead. Read the parts back and compare.
Try it yourself
- Extend the pattern to accept milliseconds, as in
2026-01-15T09:30:45.123. - Write
toLocalIso(date)that turns a date back intoYYYY-MM-DDTHH:MMtext, and check that parsing it gives the same time. - Make
parseLocalIsoalso accept2026/01/15.