CyrefJS
CCyrefJS
فا
Back to documentation
Date/Since 1.38.0

isValidDate

Checks whether a value is a valid JavaScript Date object.

isValidDate(value):value is Date

Parameters

valueunknownrequired

The value to check.

Returns

value is Date

true if the value is a Date instance with a valid timestamp; otherwise, false. The return type also acts as a TypeScript type guard.

Examples

01

Basic usage

Check whether a value is a valid Date.

TypeScript
import { isValidDate } from "@cyref/js";

const value = new Date("2026-01-15");

const result = isValidDate(value);

console.log(result);
// true
02

Invalid Date

An invalid Date object returns false because its timestamp is NaN.

TypeScript
import { isValidDate } from "@cyref/js";

const value = new Date("invalid");

const result = isValidDate(value);

console.log(result);
// false
03

Non-Date values

Values that are not Date instances return false.

TypeScript
import { isValidDate } from "@cyref/js";

console.log(isValidDate("2026-01-15"));
// false

console.log(isValidDate(123456789));
// false

console.log(isValidDate(null));
// false
04

TypeScript type guard

The value is narrowed to Date inside the conditional block.

TypeScript
import { isValidDate } from "@cyref/js";

const value: unknown = new Date("2026-05-20");

if (isValidDate(value)) {
  console.log(value.getFullYear());
  // 2026
}
05

Valid Date object

A Date object with a valid timestamp returns true.

TypeScript
import { isValidDate } from "@cyref/js";

const value = new Date("2026-08-25T12:30:00");

console.log(isValidDate(value));
// true
Built with@cyref/js·Official documentation