nepali-date-library
Version:
A Nepali Date Library for both typescript and javascript
396 lines (395 loc) • 14 kB
TypeScript
/**
* Class representing a Nepali date (Bikram Sambat)
* Provides methods to create, manipulate and format Nepali dates
*/
export declare class NepaliDate {
/** JavaScript Date object representing the equivalent Gregorian date */
timestamp: Date;
/** Nepali year (BS) */
year: number;
/** Nepali month (0-11) */
month: number;
/** Nepali day of month (1-32) */
day: number;
/**
* Creates a NepaliDate instance for the current date
*/
constructor();
/**
* Creates a NepaliDate instance from various input types
* @param date Source date as Date, NepaliDate, timestamp number, or date string
*/
constructor(date: Date | NepaliDate | number | string);
/**
* Creates a NepaliDate instance with the specified year, month, and day
* @param year Nepali year
* @param month Nepali month (0-11)
* @param day Nepali day (1-32)
*/
constructor(year: number, month: number, day: number);
/**
* Sets the internal properties based on the provided English (Gregorian) date
* @param date JavaScript Date object
* @private
*/
private setEnglishDate;
/**
* Returns the equivalent English (Gregorian) date
* @returns JavaScript Date object representing the equivalent Gregorian date
*/
getEnglishDate(): Date;
/**
* Parses a date string and updates the current instance
* @param dateString Date string in format YYYY-MM-DD, YYYY/MM/DD, or YYYY.MM.DD
* @throws Error if the date format is invalid or date is out of range
*/
parse(dateString: string): void;
/**
* Returns the Nepali year
* @returns Nepali year
*/
getYear(): number;
/**
* Returns the Nepali month (0-11)
* @returns Nepali month (0-11)
*/
getMonth(): number;
/**
* Returns the Nepali day of month
* @returns Nepali day of month (1-32)
*/
getDate(): number;
/**
* Returns the day of week (0-6, 0 = Sunday)
* @returns Day of week (0-6, 0 = Sunday)
*/
getDay(): number;
/**
* Returns the hour (0-23)
* @returns Hour (0-23)
*/
getHours(): number;
/**
* Returns the minutes (0-59)
* @returns Minutes (0-59)
*/
getMinutes(): number;
/**
* Returns the seconds (0-59)
* @returns Seconds (0-59)
*/
getSeconds(): number;
/**
* Returns the milliseconds (0-999)
* @returns Milliseconds (0-999)
*/
getMilliseconds(): number;
/**
* Returns the timestamp (milliseconds since Unix Epoch)
* @returns Timestamp in milliseconds
*/
getTime(): number;
/**
* Sets the Nepali year
* @param year Nepali year
*/
setYear(year: number): void;
/**
* Sets the Nepali month
* @param month Nepali month (0-11)
*/
setMonth(month: number): void;
/**
* Sets the Nepali day of month
* @param day Nepali day of month (1-32)
*/
setDate(day: number): void;
/**
* Sets the Nepali date components and updates the internal timestamp
* @param year Nepali year
* @param month Nepali month (0-11)
* @param date Nepali day (1-32)
*/
set(year: number, month: number, date: number): void;
/**
* Formats the Nepali date according to the specified format string
* @param formatStr Format string (see DateFormatter for syntax)
* @returns Formatted date string
*/
format(formatStr: string): string;
/**
* Returns the string representation of the Nepali date
* @returns Date string in format YYYY/MM/DD where MM is 1-indexed
*/
toString(): string;
/**
* Adds the specified number of days to the current Nepali date
* @param days Number of days to add (can be negative)
* @returns A new NepaliDate instance with the added days
*/
addDays(days: number): NepaliDate;
/**
* Adds the specified number of months to the current Nepali date
* @param months Number of months to add (can be negative)
* @returns A new NepaliDate instance with the added months
*/
addMonths(months: number): NepaliDate;
/**
* Adds the specified number of years to the current Nepali date
* @param years Number of years to add (can be negative)
* @returns A new NepaliDate instance with the added years
*/
addYears(years: number): NepaliDate;
/**
* Returns the earliest date supported by the NepaliDate class
* @returns JavaScript Date object representing the minimum supported date
*/
static minimum(): Date;
/**
* Returns the latest date supported by the NepaliDate class
* @returns JavaScript Date object representing the maximum supported date
*/
static maximum(): Date;
/**
* Returns the number of days in the current month
* @returns Number of days in the month
*/
daysInMonth(): number;
/**
* Checks if the current year is a leap year in the Nepali calendar
* @returns true if the year is a leap year, false otherwise
*/
isLeapYear(): boolean;
/**
* Calculates the number of weeks in the current month
* @returns Number of weeks in the month
*/
getWeeksInMonth(): number;
/**
* Calculates the difference between two dates in the specified unit
* @param date NepaliDate to compare with
* @param unit Unit for the difference calculation ('year', 'month', or 'day')
* @returns Difference value in the specified unit
*/
diff(date: NepaliDate, unit: 'year' | 'month' | 'day'): number;
/**
* Returns a new NepaliDate representing the start of the current day (00:00:00)
* @returns A new NepaliDate set to the start of the day
*/
startOfDay(): NepaliDate;
/**
* Returns a new NepaliDate representing the end of the current day (23:59:59.999)
* @returns A new NepaliDate set to the end of the day
*/
endOfDay(): NepaliDate;
/**
* Returns a new NepaliDate representing the start of the week containing this date
* By default, weeks start on Sunday (day 0)
* @param startOfWeek Day to consider as start of week (0-6, where 0 = Sunday, 1 = Monday, etc.)
* @returns A new NepaliDate set to the first day of the week
*/
startOfWeek(startOfWeek?: number): NepaliDate;
/**
* Returns a new NepaliDate representing the end of the week containing this date
* By default, weeks end on Saturday (day 6)
* @param startOfWeek Day to consider as start of week (0-6, where 0 = Sunday, 1 = Monday, etc.)
* @returns A new NepaliDate set to the last day of the week
*/
endOfWeek(startOfWeek?: number): NepaliDate;
/**
* Returns a new NepaliDate representing the start of the current month (1st day)
* @returns A new NepaliDate set to the first day of the month
*/
startOfMonth(): NepaliDate;
/**
* Returns a new NepaliDate representing the end of the current month (last day)
* @returns A new NepaliDate set to the last day of the month
*/
endOfMonth(): NepaliDate;
/**
* Returns a new NepaliDate representing the start of the current year (1st Baisakh)
* @returns A new NepaliDate set to the first day of the year
*/
startOfYear(): NepaliDate;
/**
* Returns a new NepaliDate representing the end of the current year (last day of Chaitra)
* @returns A new NepaliDate set to the last day of the year
*/
endOfYear(): NepaliDate;
/**
* Returns the name of the specified Nepali month
* @param month Month index (0-11)
* @param short Whether to return the short form of the month name
* @returns Month name in Nepali or English
*/
static getMonthName(month: number, short?: boolean, nepali?: boolean): string;
/**
* Returns the name of the specified day of week
* @param day Day of week (0-6, where 0 is Sunday)
* @param short Whether to return the short form of the day name
* @returns Day name in Nepali or English
*/
static getDayName(day: number, short?: boolean, nepali?: boolean): string;
/**
* Checks if the specified Nepali date is valid
* @param year Nepali year to validate
* @param month Nepali month to validate (0-11)
* @param day Nepali day to validate
* @returns true if the date is valid, false otherwise
*/
static isValid(year: number, month: number, day: number): boolean;
/**
* Checks if the current NepaliDate instance contains a valid date
* @returns true if the date is valid, false otherwise
*/
isValid(): boolean;
/**
* Generate calendar days for a given month, including trailing/leading days from adjacent months
* @param year Nepali year
* @param month Nepali month (0-11)
* @returns An object containing the calendar days for the previous month, current month, and next month.
* The object also includes the remaining days and the day objects for each month,
* with each day object containing the date and a flag indicating whether it is part of the current month.
*/
static getCalendarDays(year: number, month: number): {
prevRemainingDays: number;
prevMonth: {
year: number;
month: number;
days: number[];
};
currentMonth: {
year: number;
month: number;
days: number[];
};
nextMonth: {
year: number;
month: number;
days: number[];
};
remainingDays: number;
};
/**
* Creates a copy of the current NepaliDate instance
* @returns A new NepaliDate instance with the same date and time
*/
clone(): NepaliDate;
/**
* Checks if this date comes after the specified date
* @param date Date to compare with
* @returns true if this date is after the specified date, false otherwise
*/
isAfter(date: NepaliDate): boolean;
/**
* Checks if this date comes before the specified date
* @param date Date to compare with
* @returns true if this date is before the specified date, false otherwise
*/
isBefore(date: NepaliDate): boolean;
/**
* Checks if this date is equal to the specified date
* @param date Date to compare with
* @returns true if dates are exactly equal (year, month, day), false otherwise
*/
isEqual(date: NepaliDate): boolean;
/**
* Checks if this date is the same as the specified date for the given unit
* @param date Date to compare with
* @param unit Unit to use for comparison ('year', 'month', or 'day')
* @returns true if dates are the same for the specified unit, false otherwise
*/
isSame(date: NepaliDate, unit: 'year' | 'month' | 'day'): boolean;
/**
* Returns the start and end dates for a specific quarter in the specified year
* @param quarter Quarter number (1-4)
* @param year Nepali year (defaults to current year if not specified)
* @returns Object containing start and end dates for the specified quarter
* @throws Error if quarter is not between 1 and 4
*/
static getQuarter(quarter: number, year?: number): {
start: NepaliDate;
end: NepaliDate;
};
/**
* Returns the quarter number (1-4) for the current date
* @returns Quarter number (1-4)
*/
getCurrentQuarter(): number;
/**
* Returns all quarters for a specified fiscal year
* @param year Fiscal year (defaults to current fiscal year if not specified)
* @returns Object containing start and end dates for each quarter of the fiscal year
*/
static getQuarters(year?: number): {
Q1: {
start: NepaliDate;
end: NepaliDate;
};
Q2: {
start: NepaliDate;
end: NepaliDate;
};
Q3: {
start: NepaliDate;
end: NepaliDate;
};
Q4: {
start: NepaliDate;
end: NepaliDate;
};
};
/**
* Returns the current fiscal year based on the current date
* Nepali fiscal year starts from Shrawan 1st (month 3, day 1)
* @returns Current fiscal year
*/
static getCurrentFiscalYear(): number;
/**
* Returns the start and end dates for a specific fiscal year quarter
* @param quarter Fiscal year quarter number (1-4)
* @param fiscalYear Fiscal year (defaults to current fiscal year if not specified)
* @returns Object containing start and end dates for the specified fiscal year quarter
* @throws Error if quarter is not between 1 and 4
*/
static getFiscalYearQuarter(quarter: number, fiscalYear?: number): {
start: NepaliDate;
end: NepaliDate;
};
/**
* Returns the current fiscal year quarter number (1-4) for the current date
* @returns Current fiscal year quarter number (1-4)
*/
getCurrentFiscalYearQuarter(): number;
/**
* Returns the start and end dates of the current fiscal year quarter
* @returns Object containing start and end dates of the current fiscal year quarter
*/
getCurrentFiscalYearQuarterDates(): {
start: NepaliDate;
end: NepaliDate;
};
/**
* Returns all quarters for a specified fiscal year
* @param fiscalYear Fiscal year (defaults to current fiscal year if not specified)
* @returns Object containing start and end dates for each quarter of the fiscal year
*/
static getFiscalYearQuarters(fiscalYear?: number): {
Q1: {
start: NepaliDate;
end: NepaliDate;
};
Q2: {
start: NepaliDate;
end: NepaliDate;
};
Q3: {
start: NepaliDate;
end: NepaliDate;
};
Q4: {
start: NepaliDate;
end: NepaliDate;
};
};
}