Skip to main content

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โ€‹

ValueResult
new Date()checkmark true
new Date('invalid')checkmark true (Invalid Date is still Date)
Date.now()cross false (number)
'2024-01-01'cross false
1704067200000cross 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());
}