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
| Parameter | Type | Required | Description |
|---|---|---|---|
date | NepaliDate | Yes | Target NepaliDate instance to compare against. |
unit | "year" | "month" | "day" | Yes | Unit of granularity for comparison. |
Return Value
- Type:
boolean - Returns
trueif both dates are identical within the specified unit boundary; otherwise returnsfalse.
Comparison Rules
"year": Evaluatesthis.year === date.year."month": Evaluatesthis.year === date.year && this.month === date.month."day": Evaluatesthis.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")); // trueComparing 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")); // trueRelated
date.isEqual(): Strict calendar day equality.date.diff(): Measure quantitative difference between dates.
Advertisement
date.isEqual()
Check whether two date instances share the exact same calendar date coordinates regardless of time.
date.isValid()
Validate whether the current NepaliDate instance represents a legitimate calendar date within supported bounds.