@pyyupsk/fdu

API ReferenceComparison

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): boolean

Parameters

  • 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);  // false

Same 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);  // true

Sort 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 15

Check 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

  • isAfter() - Check if after another date
  • isSame() - Check if same as another date
  • diff() - Calculate difference between dates