Skip to main content

Check Date-like objects

isDateLike

Detects date-like objects from multiple popular date/time libraries.

Features

  • Detects native JavaScript Date objects
  • Supports popular date libraries:
    • Moment.js
    • Dayjs
    • Luxon
    • JS-Joda
    • Temporal
    • Chronos
  • Type-safe TypeScript implementation
  • Zero dependencies

Usage

Basic Usage

import { isDateLike } from 'nhb-toolbox';

const nativeDate = new Date();
const momentDate = moment(); // Moment.js
const luxonDate = DateTime.now(); // Luxon
const dayjsDate = dayjs(); // dayjs
const chronosDate = chronos(); // chronos

console.log(isDateLike(nativeDate)); // true
console.log(isDateLike(momentDate)); // true
console.log(isDateLike(luxonDate)); // true
console.log(isDateLike(dayjsDate)); // true
console.log(isDateLike(chronosDate)); // true
console.log(isDateLike('2023-01-01')); // false`}

Supported Libraries

The function recognizes these date-like objects:

LibraryDetection Criteria
Native Dateinstanceof Date
ChronosHas format(), toJSON(), and toISOString() methods
DayjsSame as Chronos
Moment.jsSame as Chronos
LuxonHas toISO(), toFormat() methods and isValid property
JS-JodaHas plus(), minus(), equals(), and getClass() methods
TemporalHas toJSON(), toString() and specific constructor names

API Reference

Function Signature

function isDateLike(value: unknown): boolean;

Parameters

ParameterTypeDescription
valueunknownThe value to be checked

Returns

boolean - true if the value is a date-like object, false otherwise

Examples

Checking Different Value Types

import { DateTime } from 'luxon';
import { Temporal } from '@js-temporal/polyfill';

isDateLike(new Date()); // true
isDateLike(DateTime.now()); // true (Luxon)
isDateLike(Temporal.Now.instant()); // true
isDateLike({}); // false
isDateLike(null); // false
isDateLike('2023-01-01'); // false`}

Notes

  • The function performs duck-typing for library objects rather than checking instanceof
  • For maximum reliability with custom date objects, ensure they implement the expected methods
  • Returns false for date strings - this only checks for date objects