Nepali DateNepali DateBikram Sambat Date Engine
Methods

date.isSame()

Check whether two dates match across a specified granularity such as year, month, or day.

The isSame method compares the current NepaliDate instance with another date at a specified level of granularity, such as year, month, day, hour, or minute. It returns true if both dates match within that specific unit, simplifying range checks and calendar filters.


Import

import { NepaliDate } from "nepali-date-library";

Signature

isSame(date: NepaliDate, unit: "year" | "month" | "day"): boolean;

Parameters

ParameterTypeRequiredDescription
dateNepaliDateYesTarget NepaliDate instance to compare against.
unit"year" | "month" | "day"YesUnit of granularity for comparison.

Return Value

  • Type: boolean
  • Returns true if both dates are identical within the specified unit boundary; otherwise returns false.

Comparison Rules

  • "year": Evaluates this.year === date.year.
  • "month": Evaluates this.year === date.year && this.month === date.month.
  • "day": Evaluates this.isEqual(date) (matching year, month, and day).

Exceptions

Throws an Error if an invalid unit string is supplied:

"Invalid unit for comparison"

Examples

Comparing Same Month

import { NepaliDate } from "nepali-date-library";

const date1 = new NepaliDate("2081-03-05");
const date2 = new NepaliDate("2081-03-25");
const date3 = new NepaliDate("2080-03-05");

console.log(date1.isSame(date2, "month")); // true (both Ashadh 2081)
console.log(date1.isSame(date3, "month")); // false (different years)

Comparing Same Year

import { NepaliDate } from "nepali-date-library";

const date1 = new NepaliDate("2081-01-01");
const date2 = new NepaliDate("2081-12-30");

console.log(date1.isSame(date2, "year")); // true

Comparing Same Day

import { NepaliDate } from "nepali-date-library";

const date1 = new NepaliDate("2081-01-15");
const date2 = new NepaliDate("2081-01-15");

console.log(date1.isSame(date2, "day")); // true

Advertisement

On this page