Lodash _.isDate() method
What you’ll learn
- How
_.isDate(value)detects Date instances via the built-in object tag. - Why numeric timestamps and ISO strings fail until you construct
Dateobjects. - That “Invalid Date” values remain Dates—use
getTime()when validating. - When lodash complements plain
typeof/instanceofchecks in pipelines.
Prerequisites
Comfort with JavaScript Date construction and parsing JSON timestamps into real dates.
- You understand
typeof new Date()is"object", not"date". - You can run Try-it labs with the lodash CDN snippet.
Overview
Use _.isDate inside lodash-heavy services when unknown payloads might carry Dates, epoch numbers, or ISO strings—and you want a single helper that mirrors the official Lang semantics.
Typed objects
Positive only for values tagged as Dates, not bare strings or numbers.
Parse first
Normalize API strings with new Date(...) before lodash sees them.
Invalid ≠ absent
Invalid Date objects still pass—validate wall-clock meaning separately.
Syntax
_.isDate(value) - value: any value to test.
- Returns:
trueifvalueis classified as a Date object; otherwisefalse.
Constructed Date instances
Fresh Dates from the constructor—whether “now” or parsed strings—are recognized immediately.
import isDate from "lodash/isDate";
isDate(new Date()); // true
isDate(new Date("2024-06-01")); // true Strings, timestamps, and plain objects
Serialization layers often expose ISO strings or epoch milliseconds—lodash keeps these distinct until you parse.
import isDate from "lodash/isDate";
isDate("2024-06-01"); // false
isDate(1717200000000); // false
isDate({ year: 2024, month: 6 }); // false Invalid Date objects still match
Parsing failures yield Date objects whose clock is NaN—_.isDate stays true; gate business logic with getTime().
import isDate from "lodash/isDate";
var bad = new Date("totally-not-a-date");
isDate(bad); // true
Number.isNaN(bad.getTime()); // true 📋 _.isDate vs related checks
| API | Matches |
|---|---|
_.isDate(x) | Built-in Date instances (including Invalid Date objects). |
_.isString(x) | Unicode strings—often how APIs ship timestamps before parsing. |
_.isNumber(x) | Numeric primitives such as epoch milliseconds. |
Number.isFinite(x.getTime()) | Calendar validity after you already hold a Date. |
Pitfalls to avoid
ISO strings stay strings
JSON.parse does not revive Dates automatically—map fields through new Date when needed.
typeof cannot spot Dates
Both Dates and plain objects read as "object"; lodash isolates the Date tag without manual string checks.
Type ≠ meaningful instant
Combine _.isDate with getTime(), range checks, or timezone-aware utilities for UX-critical scheduling.
❓ FAQ
Summary
- Purpose: classify values as Date objects inside lodash pipelines.
- Remember: strings and numbers require parsing; Invalid Date still passes
_.isDate. - Next: explore more on Lodash _.isElement().
_.isDate relies on the internal [object Date] tag. An “Invalid Date” produced by new Date("???") is still a Date object, so lodash returns true—validate with Number.isNaN(value.getTime()) when you care about real instants.
6 people found this page helpful
