isBefore()
Learn how to use the isBefore() method to check if a date is before another date. Validate date ranges, check past dates, sort dates chronologically, schedule events, and check deadlines with comprehensive examples for event management and date validation.
Checks if this date is before another date.
Syntax
.isBefore(other: FduInstance): booleanParameters
other: FduInstance- Date to compare with
Returns
boolean - True if this date is before the other date
Examples
Basic Comparison
import { fdu } from '@pyyupsk/fdu';
const date1 = fdu('2024-01-15');
const date2 = fdu('2024-01-20');
date1.isBefore(date2); // true
date2.isBefore(date1); // falseSame Date
const date1 = fdu('2024-01-15');
const date2 = fdu('2024-01-15');
date1.isBefore(date2); // false (same date)With Time
const morning = fdu('2024-01-15T09:00:00');
const afternoon = fdu('2024-01-15T14:00:00');
morning.isBefore(afternoon); // true
const sameTime = fdu('2024-01-15T09:00:00');
morning.isBefore(sameTime); // false (exact same time)Common Patterns
Check if date is in the past
const date = fdu('2023-01-15');
const now = fdu();
if (date.isBefore(now)) {
console.log('This date is in the past');
}Validate date range
function isInRange(date: FduInstance, start: FduInstance, end: FduInstance): boolean {
return !date.isBefore(start) && date.isBefore(end);
}
const date = fdu('2024-06-15');
const start = fdu('2024-01-01');
const end = fdu('2024-12-31');
isInRange(date, start, end); // trueSort dates
const dates = [
fdu('2024-03-15'),
fdu('2024-01-20'),
fdu('2024-02-10')
];
dates.sort((a, b) => a.isBefore(b) ? -1 : 1);
// Results in chronological order: Jan 20, Feb 10, Mar 15Check deadline
const deadline = fdu('2024-12-31');
const today = fdu();
if (today.isBefore(deadline)) {
const daysLeft = deadline.diff(today, 'day');
console.log(`${daysLeft} days until deadline`);
} else {
console.log('Deadline has passed');
}Event scheduling
const eventDate = fdu('2024-12-25');
const now = fdu();
if (now.isBefore(eventDate)) {
console.log('Event is upcoming');
} else {
console.log('Event has already occurred');
}See Also
subtract()
Learn to use the subtract() method to subtract years, months, weeks, days, hours, minutes, seconds, and milliseconds from dates. Master immutable date manipulation, chaining operations, handling month/year edge cases, and calculating historical dates, time ranges, and deadlines with practical examples.
isAfter()
Learn how to use the isAfter() method to check if a date is after another date. Validate chronological order, check future dates, verify expiration, find latest dates, and implement time tracking with comprehensive examples for event scheduling and deadline management.