isSameOrAfter()
Check if a date is the same as or after another date with optional granularity. Ideal for availability checking, future date validation, and filtering upcoming events.
Checks if this date is the same as or after another date, optionally at a specific granularity.
Syntax
.isSameOrAfter(other: FduInstance, unit?: UnitType): booleanParameters
other: FduInstance- Date to compare withunit?: UnitType- Optional unit for granularity comparison
Returns
boolean - True if this date is the same as or after the other date
Examples
Basic Comparison
import { fdu } from "@pyyupsk/fdu";
const date1 = fdu("2024-01-20");
const date2 = fdu("2024-01-15");
const date3 = fdu("2024-01-20");
date1.isSameOrAfter(date2); // true (after)
date1.isSameOrAfter(date3); // true (same)
date2.isSameOrAfter(date1); // false (before)Granular Comparison
Compare at specific units of time:
const date1 = fdu("2024-01-15T14:45:00");
const date2 = fdu("2024-01-15T10:30:00");
date1.isSameOrAfter(date2, "day"); // true (same day)
date1.isSameOrAfter(date2, "hour"); // true (14 >= 10)
date1.isSameOrAfter(date2, "month"); // true (same month)Availability Checking
function isAvailable(date: FduInstance, startDate: FduInstance): boolean {
return date.isSameOrAfter(startDate, "day");
}
const requestedDate = fdu("2024-01-20");
const availableFrom = fdu("2024-01-15");
isAvailable(requestedDate, availableFrom); // trueFuture Date Validation
function isFutureOrToday(date: FduInstance): boolean {
return date.isSameOrAfter(fdu(), "day");
}
const eventDate = fdu("2024-12-25");
isFutureOrToday(eventDate); // true if today is before or on Dec 25, 2024Filter Upcoming Events
const events = [
{ date: fdu("2024-01-10"), name: "Past Event" },
{ date: fdu("2024-01-15"), name: "Today Event" },
{ date: fdu("2024-01-20"), name: "Future Event" },
];
const today = fdu("2024-01-15");
const todayOrFuture = events.filter((e) => e.date.isSameOrAfter(today, "day"));
// [Today Event, Future Event]Age Verification
function isOldEnough(birthdate: FduInstance, minimumAge: number): boolean {
const minimumBirthdate = fdu().subtract(minimumAge, "year");
return minimumBirthdate.isSameOrAfter(birthdate, "day");
}
const userBirthdate = fdu("2000-05-15");
isOldEnough(userBirthdate, 21); // true if user is 21 or olderSupported Units
year/y- Same or later calendar yearmonth/M- Same or later calendar monthday/d- Same or later calendar dayhour/h- Same or later hourminute/m- Same or later minutesecond/s- Same or later second
See Also
- isSameOrBefore() - Check if same or before another date
- isSame() - Check if same as another date
- isAfter() - Check if strictly after another date
isSameOrBefore()
Check if a date is the same as or before another date with optional granularity. Perfect for deadline checking, date range validation, and filtering past events.
diff()
Learn how to calculate differences between dates using the diff() method. Calculate age, duration, time elapsed, and date differences in years, months, weeks, days, hours, minutes, seconds, and milliseconds with practical examples for deadline warnings, relative time, working hours, and subscription duration tracking.