isDate()
isDate(
value):value is Date
Checks if a value is a Date instance.
note
Does not validate if the Date is valid (use isValidDate for that).
Parametersโ
value: unknownโ
The value to check.
Returns: value is Dateโ
true if the value is a Date, false otherwise.
Sinceโ
2.0.0
Exampleโ
isDate(new Date()); // => true
isDate(new Date('invalid')); // => true (still a Date instance)
isDate('2024-01-01'); // => false
isDate(Date.now()); // => false (number)
How it works?โ
Type guard that checks if a value is a Date instance.
Type Narrowingโ
Common Checksโ
| Value | Result |
|---|---|
new Date() | true |
new Date('invalid') | true (Invalid Date is still Date) |
Date.now() | false (number) |
'2024-01-01' | false |
1704067200000 | false |
Noteโ
isDate returns true for Invalid Date. Use !isNaN(date.getTime()) to check validity.
Use Casesโ
Validate timestamps ๐โ
Ensure a value is a valid Date object before formatting or comparison. Essential for time-sensitive logic and scheduling applications.
if (isDate(timestamp) && !isNaN(timestamp.getTime())) {
console.log(timestamp.toISOString());
}
true
false (number)