isBeforeDate
Checks whether the target date is before the compare date.
The inclusive option allows comparing dates that are equal as well.
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
compareDateis provided, returnstrueif 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
compareDateis provided, returnstrueif 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
inclusiveistrue, 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