Programming
Date Object
Object Identification
Coding Tips
JavaScript

How to check whether an object is a date?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In JavaScript, checking whether something is a date means answering two related but different questions. First, is the value actually a Date object. Second, does it represent a valid date value rather than an invalid Date. Good code usually needs to think about both.

Check whether the value is a Date instance

If you only need to know whether the object was created as a JavaScript Date, instanceof is the usual first check.

javascript
1const a = new Date();
2const b = "2024-01-01";
3
4console.log(a instanceof Date);
5console.log(b instanceof Date);

This works well in ordinary same-realm code, such as values created in the same browser window or Node.js process.

A more robust object-tag check

In some situations, especially across iframes or other realms, instanceof Date can be less reliable because the constructor identity may differ. A more robust structural check is:

javascript
1function isDateObject(value) {
2  return Object.prototype.toString.call(value) === "[object Date]";
3}
4
5console.log(isDateObject(new Date()));
6console.log(isDateObject("2024-01-01"));

This tells you whether the object carries the built-in Date tag, which is often a better test when values may come from a different execution context.

Being a Date object is not the same as being valid

JavaScript can create an invalid Date object:

javascript
1const bad = new Date("not a real date");
2
3console.log(bad instanceof Date);
4console.log(bad.toString());

That object is still a Date, but it does not represent a usable timestamp.

So if your real goal is "is this a usable date," check both type and validity:

javascript
1function isValidDate(value) {
2  return value instanceof Date && !Number.isNaN(value.getTime());
3}
4
5console.log(isValidDate(new Date()));
6console.log(isValidDate(new Date("not a real date")));
7console.log(isValidDate("2024-01-01"));

getTime() returns NaN for invalid dates, which makes it a practical validity check.

Choose the check that matches the requirement

A useful rule is:

  • use instanceof Date in normal same-realm application code
  • use Object.prototype.toString.call(value) when cross-realm robustness matters
  • also check getTime() when validity matters, not just object type

This distinction matters because many bugs come from mixing up "date-shaped object" and "valid timestamp."

Do not confuse strings with Date objects

A string such as "2024-01-01" may represent a date conceptually, but it is not a Date object. That means this check should fail:

javascript
console.log("2024-01-01" instanceof Date);

If your application accepts either strings or Date objects, parse or normalize the input first rather than weakening the type check.

For example:

javascript
1function normalizeDate(value) {
2  if (value instanceof Date) {
3    return !Number.isNaN(value.getTime()) ? value : null;
4  }
5
6  if (typeof value === "string") {
7    const parsed = new Date(value);
8    return !Number.isNaN(parsed.getTime()) ? parsed : null;
9  }
10
11  return null;
12}
13
14console.log(normalizeDate("2024-01-01"));

That is clearer than pretending strings and Date objects are the same kind of input.

Common Pitfalls

The most common mistake is checking only whether the value is a Date object and forgetting that invalid Date instances exist.

Another common issue is using instanceof Date across different realms and being surprised when it fails.

People also confuse date strings with actual Date objects. A string may describe a date, but it is still just a string until you parse it.

Finally, avoid type checks that are looser than your real requirement. If you need a valid timestamp, test validity explicitly.

Summary

  • 'instanceof Date is the standard JavaScript check for a Date object.'
  • 'Object.prototype.toString.call(value) is more robust across realms.'
  • A Date object can still be invalid, so check getTime() when validity matters.
  • Date strings are not the same thing as Date instances.
  • Decide whether you need type checking, validity checking, or input normalization.

Course illustration
Course illustration

All Rights Reserved.