@pyyupsk/fdu

API ReferenceComparison

isSame()

Master the isSame() method to compare dates with granular precision. Check if dates match by year, month, day, hour, minute, or second. Filter events, validate birthdays, check if today, group dates by month, and implement same time of day checks with practical examples.

Checks if this date is the same as another date, optionally at a specific granularity.

Syntax

.isSame(other: FduInstance, unit?: UnitType): boolean

Parameters

  • other: FduInstance - Date to compare with
  • unit?: UnitType - Optional unit for granularity comparison

Returns

boolean - True if dates are the same (at the specified granularity)

Examples

Exact Comparison

Without a unit, compares exact timestamps:

import { fdu } from '@pyyupsk/fdu';

const date1 = fdu('2024-01-15T10:30:00');
const date2 = fdu('2024-01-15T10:30:00');
const date3 = fdu('2024-01-15T14:45:00');

date1.isSame(date2);  // true (exact same time)
date1.isSame(date3);  // false (different times)

Granular Comparison

Compare at specific units of time:

const date1 = fdu('2024-01-15T10:30:00');
const date2 = fdu('2024-01-15T14:45:00');

// Same day, different times
date1.isSame(date2, 'day');     // true
date1.isSame(date2, 'month');   // true
date1.isSame(date2, 'year');    // true
date1.isSame(date2, 'hour');    // false
date1.isSame(date2, 'minute');  // false

Different Days

const date1 = fdu('2024-01-15T10:30:00');
const date2 = fdu('2024-01-16T10:30:00');

date1.isSame(date2, 'day');     // false
date1.isSame(date2, 'month');   // true
date1.isSame(date2, 'year');    // true

Different Months

const date1 = fdu('2024-01-15');
const date2 = fdu('2024-02-15');

date1.isSame(date2, 'month');   // false
date1.isSame(date2, 'year');    // true

Different Years

const date1 = fdu('2024-01-15');
const date2 = fdu('2025-01-15');

date1.isSame(date2, 'year');    // false

Supported Units

  • year / y - Same calendar year
  • month / M - Same calendar month and year
  • day / d - Same calendar day
  • hour / h - Same hour
  • minute / m - Same minute
  • second / s - Same second

Common Patterns

Check if dates are on the same day

const date1 = fdu('2024-01-15T09:00:00');
const date2 = fdu('2024-01-15T17:00:00');

if (date1.isSame(date2, 'day')) {
  console.log('These events are on the same day');
}

Check if today

function isToday(date: FduInstance): boolean {
  return date.isSame(fdu(), 'day');
}

const someDate = fdu('2024-01-15');
isToday(someDate);  // true if today is Jan 15, 2024

Group by month

const dates = [
  fdu('2024-01-05'),
  fdu('2024-01-15'),
  fdu('2024-02-10'),
  fdu('2024-02-20')
];

const januaryDates = dates.filter(d =>
  d.isSame(fdu('2024-01-01'), 'month')
);
// [2024-01-05, 2024-01-15]

Check if same week

const date1 = fdu('2024-01-15');  // Monday
const date2 = fdu('2024-01-18');  // Thursday

// Note: isSame doesn't have 'week' unit, use alternative:
const startOfWeek1 = date1.subtract(date1.day(), 'day');
const startOfWeek2 = date2.subtract(date2.day(), 'day');

startOfWeek1.isSame(startOfWeek2, 'day');  // true

Birthday check

function isBirthday(birthdate: FduInstance): boolean {
  const today = fdu();
  return birthdate.month() === today.month() &&
         birthdate.date() === today.date();
}

const birthday = fdu('1990-05-15');
isBirthday(birthday);  // true if today is May 15

Same time of day check

const time1 = fdu('2024-01-15T14:30:00');
const time2 = fdu('2024-02-20T14:30:00');

const sameHour = time1.isSame(time2, 'hour') &&
                 time1.minute() === time2.minute();
// true (both at 14:30)

Filter events by year

const events = [
  { date: fdu('2023-05-15'), name: 'Event A' },
  { date: fdu('2024-01-20'), name: 'Event B' },
  { date: fdu('2024-06-10'), name: 'Event C' }
];

const year2024 = fdu('2024-01-01');
const events2024 = events.filter(e =>
  e.date.isSame(year2024, 'year')
);
// [Event B, Event C]

See Also

  • isBefore() - Check if before another date
  • isAfter() - Check if after another date
  • diff() - Calculate difference between dates