Skip to main content

isBeforeDate

Checks whether the target date is before the compare date.

The inclusive option allows comparing dates that are equal as well.


Code

🔗 View source code


Interface

typescript
interface IsBeforeDateParams {
targetDate: string | number | Date;
compareDate?: string | number | Date;
inclusive?: boolean;
}
typescript
function isBeforeDate({
targetDate,
compareDate = new Date(),
inclusive = false,
}: IsBeforeDateParams): boolean;

Usage

without compareDate

  • When no compareDate is provided, returns true if the target date is before the current date.
typescript
import { isBeforeDate } from '@modern-kit/utils';

// When the current date is January 1, 2025 00:00:00
isBeforeDate({ targetDate: new Date('2024-12-31') }); // true
isBeforeDate({ targetDate: new Date('2025-01-02') }); // false

// String format is also accepted.
isBeforeDate({ targetDate: '2024-12-31' }); // true

with compareDate

  • When a compareDate is provided, returns true if the target date is before the compare date.
typescript
import { isBeforeDate } from '@modern-kit/utils';

isBeforeDate({ targetDate: new Date('2024-12-31'), compareDate: new Date('2025-01-01') }); // true
isBeforeDate({ targetDate: new Date('2025-01-01'), compareDate: new Date('2024-12-31') }); // false

// String format is also accepted.
isBeforeDate({ targetDate: '2024-12-31', compareDate: '2025-01-01' }); // true

inclusive option

  • When inclusive is true, equal dates are also considered as "before".
typescript
import { isBeforeDate } from '@modern-kit/utils';

isBeforeDate({
targetDate: new Date('2025-01-01'),
compareDate: new Date('2025-01-01'),
inclusive: false,
});
// false

isBeforeDate({
targetDate: new Date('2025-01-01'),
compareDate: new Date('2025-01-01'),
inclusive: true,
});
// true