Emmanuel Akinfulubi

JavaScriptDate

Check if a date is valid in JavaScript

Intermediate · 3 min read

Dates often arrive as text: from a form, a file or another server. When that text is nonsense, JavaScript doesn't throw an error. It hands you a Date that is broken inside, and if you don't catch it, it spreads Invalid Date and NaN through your page. In this tutorial you will write isValidDate(value), a check that tells a usable date from a broken one.

It is a short function, but there are three wrong answers that look right, and you will see all of them fail. You need to know how to make a date and what typeof and instanceof do in general terms.

What an invalid date is

Every working date holds a number inside: the milliseconds since the start of 1970, which you read with getTime(). When new Date can't understand what you gave it, it still makes a Date object, but the number inside is NaN, which means "not a number".

JavaScript

const bad = new Date('not a date');
const good = new Date(2026, 0, 15);
console.log(good.getTime()); // 1768431600000 in Lagos; the number depends on your zone
console.log(bad.getTime());  // NaN
console.log(String(bad));    // 'Invalid Date'

So the job is to spot that NaN.

NaN is not equal to anything

NaN has a famous quirk: it is not equal to itself.

JavaScript

console.log(NaN === NaN);          // false
console.log(Number.isNaN(NaN));    // true
console.log(Number.isNaN(42));     // false

Because === can't find it, JavaScript gives you Number.isNaN, which returns true only for NaN.

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

13 Detecting an Invalid Date

fair

Write a check for whether a value is a usable date. An unparseable string still gives you a Date object, so instanceof alone proves nothing.

Stuck? Three hints, smallest first
  1. 1. An invalid date is a real Date whose internal timestamp is NaN.
  2. 2. Number.isNaN(d.getTime()) is the test. d === ‘Invalid Date’ compares an object with a string and is always false.
  3. 3. Remember that NaN === NaN is false, so comparing the timestamp to NaN does not work either.

Step 1: wrong answer one, instanceof

value instanceof Date asks "was this made by Date?". That sounds like a validity check:

Step 1

console.log(good instanceof Date); // true
console.log(bad instanceof Date);  // true
console.log(typeof bad);           // 'object'

The broken date is still a real Date object, so this check passes it. It proves the type, not that the date works.

Step 2: wrong answer two, comparing with the text

String(bad) prints Invalid Date, so why not compare with that?

Step 2

console.log(bad === 'Invalid Date'); // false

bad is an object and 'Invalid Date' is a string. === never treats an object and a string as equal, so this is false for every date, valid or not. A check that never fires looks like it is working and protects nothing.

Step 3: wrong answer three, comparing with NaN

Then check the number inside instead:

Step 3

console.log(bad.getTime() === NaN); // false

This fails too, because NaN === NaN is always false, as you saw above.

Step 4: the right test

Use Number.isNaN on the timestamp. Keep instanceof as a guard in front, so that a string or null is turned away before .getTime() is called on it:

Step 4

const isValidDate = (value) =>
  value instanceof Date && !Number.isNaN(value.getTime());
console.log(isValidDate(good)); // true
console.log(isValidDate(bad));  // false
console.log(isValidDate('2026-01-15')); // false
console.log(isValidDate(null)); // false

&& stops as soon as one side is false. For '2026-01-15' and null, instanceof Date is false, so .getTime() is never reached. Without that guard, null.getTime() would throw a TypeError.

Notice that '2026-01-15' is rejected because it is text, not a date. The function checks a Date you already have. Turning text into a date is a separate step.

Step 5: a quiet surprise with null

What about new Date(null)?

Step 5

const fromNull = new Date(null);
console.log(isValidDate(fromNull)); // true
console.log(fromNull.getTime());    // 0

null turns into the number 0, and 0 milliseconds is a real moment: midnight UTC on 1 January 1970. So a missing value can turn into a date that looks real and passes the check. new Date(undefined) behaves differently, and gives an invalid date. Check for missing input before you build the date.

The full solution

The panel has isValidDate and checks for all three wrong answers, the null case and a rejected string. It also prints new Date('2026-02-30') without testing it, because JavaScript engines don't agree on whether that text is invalid or rolls into March. Press Run to see the results.

The answer

const isValidDate = (value) =>
  value instanceof Date && !Number.isNaN(value.getTime());

const bad = new Date('not a date');
const good = new Date(2026, 0, 15);

console.log('bad  ->', String(bad), '| valid:', isValidDate(bad));
console.log('good ->', good.toDateString(), '| valid:', isValidDate(good));
console.log('typeof bad:', typeof bad, '| instanceof Date:', bad instanceof Date);
console.log('new Date(undefined) valid?', isValidDate(new Date(undefined)));

// Out-of-range days in a string are NOT reliable ground: engines disagree about
// whether '2026-02-30' is invalid or is rolled into 2 March. Shown, not asserted.
console.log("new Date('2026-02-30') ->", String(new Date('2026-02-30')));

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('an unparseable string gives an invalid date', isValidDate(bad), false);
test('a real date passes', isValidDate(good), true);
test('the invalid one is still a Date object', bad instanceof Date, true);
test('its timestamp is NaN', Number.isNaN(bad.getTime()), true);
test('comparing the object to the text never works', bad === 'Invalid Date', false);
test('even though that is exactly what it prints', String(bad), 'Invalid Date');
test('and comparing the timestamp to NaN fails too, since NaN !== NaN', bad.getTime() === NaN, false);
test('new Date(null) is the epoch, not invalid', new Date(null).getTime(), 0);
test('and a string is rejected before getTime is ever reached', isValidDate('2026-01-15'), false);

Three wrong answers, each of which looks reasonable. d instanceof Date is true for an invalid date. d === 'Invalid Date' compares an object with a string, so it is false for every date, valid or not — a check that never fires is worse than no check. And d.getTime() === NaN is false by definition, because NaN is not equal to itself.

Number.isNaN(d.getTime()) is the whole answer, guarded by an instanceof so that a string or null does not throw on .getTime(). Note new Date(null): null converts to the number 0, so you get 1 January 1970 rather than an error, which is a quiet way for a missing value to become a real-looking date.

Common mistakes

  • Trusting instanceof Date. Invalid dates are still Date objects.
  • Comparing with 'Invalid Date' or NaN. The first compares an object with a string, the second compares with something that equals nothing. Both are always false.
  • Calling .getTime() on any value. A string has no getTime, and null throws. Check instanceof Date first.

Try it yourself

  1. Check isValidDate(new Date(undefined)) and isValidDate(new Date('')). Predict both first.
  2. Write parseOrNull(text) that returns a valid Date, or null if the text can't be read.
  3. Use filter with isValidDate to keep only the good dates in a mixed array.

Keep practising

Newsletter

New writing, when it lands.

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