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.
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:
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:
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:
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 Datein 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:
If your application accepts either strings or Date objects, parse or normalize the input first rather than weakening the type check.
For example:
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 Dateis the standard JavaScript check for aDateobject.' - '
Object.prototype.toString.call(value)is more robust across realms.' - A
Dateobject can still be invalid, so checkgetTime()when validity matters. - Date strings are not the same thing as
Dateinstances. - Decide whether you need type checking, validity checking, or input normalization.

