The Nepali Date API offers functions for converting between Nepali Bikram Sambat and Gregorian dates, along with date formatting, parsing, and manipulation. It simplifies working with the Nepali calendar, providing accurate tools for date conversion and handling.

import { NepaliDate } from '@zener/nepali-datepicker-react';

export default function App() {
  const date = new NepaliDate();
  const year = date.getFullYear();

  return '...'
}
Methods

The following methods are available:

Method
Return Type
Description
getFullYear()
string | number
Gets the year, using local time.
getMonth()
string | number
Gets the month of a Date object.
getMonthName()
string
Gets the name of the month of a Date object.
getDate()
string | number
Gets the day-of-the-month.
getDay()
string | number
Gets the day of the week.
getDayName()
string
Gets the short name of day of the week.
getDayNameFull()
string
Gets the full name of day of the week.
now()
NepaliDate
Returns the current nepali date.
parseDate(date:string)
{year : number, date : number, day : number}
Returns the parsed date object from string of format YYYY-MM-DD.
toAD()
Object
Returns the year, month and date in AD
toADasDate()
DateObject
Returns the date object for Gregorian Calendar.
toJson()
Nepali date in JSON format.
Returns the date object for Nepali date as JSON.
toString()
Nepali date in string format.
Returns the date object for Nepali date as string in format YYYY-MM-DD.
format(format:string)
Nepali date in provided format pattern.
Formats date in given string format pattern. eg. (YYYY/MM/DD), (MMMM DD, YYYY, dddd).

API to format date based on the string token provided.

new NepaliDate().format("MMMM DD, YYYY") //Output: Kartik 07, 2081

new NepaliDate().format("MMMM D, YYYY, dddd") //Output: Kartik 7, 2081, Wednesday

new NepaliDate().format("MMMM D, YYYY, dddd", 'np') //Output: कार्तिक ७, २०८१, बुधबार

new NepaliDate("2070-10-10").format("MMMM D, YYYY, dddd") //Output: Magh 10, 2070, Friday

//For escaping, place text inside square brackets, e.g. [MMMM]
new NepaliDate().format("[MMMM] MMMM D, YYYY, dddd") //Output: MMMM Kartik 7, 2081, Wednesday

The following are the available formats:

Format
Output
Description
d
0-6
The day of the week, with Sunday as 0
dd
Su-Sa
The min name of the day of the week
ddd
Sun-Sat
The short name of the day of the week
dddd
Sunday-Saturday
The full name of the day of the week
D
1-31 for AD and 1-32 for BS
The day of the month
DD
01-31 for AD and 01-32 for BS
The day of the month in 2 digits.
M
1-12
The month of the year.
MM
01-12
The month of the year in 2 digits.
MMM
Bai-Cha
Short name of month
MMMM
Baishak-Chaitra
Full name of month
YY
81
Year in two digits.
YYYY
2081
Year in four digits.

API to manipulate date.

const od = new NepaliDate(); // original date

// addition
const ad = date.add(1, "day"); // date after addition

// subtraction
const sd = date.subtract(1, "day"); // date after subtraction

/** Note:
The original date remains unmodified during add and subtract operation.
These operations returns new instance of NepaliDate.
**/


//setDate
od.setDate(1) 
/*** returns undefined but this operation modifies 
the original date by setting the day of month to desired value.
**/


//setMonth
od.setMonth(1) 
/*** returns undefined but this operation modifies 
the original date by setting the month of the year to desired value.
**/


//setFullYear
od.setFullYear(2070) 
/*** returns undefined but this operation modifies 
the original date by setting the year to desired value.
**/

The available units are 'day', 'd', 'week', 'w', 'month', 'm', 'year', 'y'.

The following are the available date manipulation methods:

Method
Return Type
Description
add(value:number, type:unit)
NepaliDate
Adds the given value to the date and returns new NepaliDate instance.
subtract(value:number, type:unit)
NepaliDate
Subtract the given value from the date and returns new NepaliDate instance.
setDate(value:number)
undefined
This method of NepaliDate instances changes the day of the month for this date.
setMonth(value:number)
undefined
This method of NepaliDate instances changes the month of the year for this date.
setFullYear(value:number)
undefined
This method of NepaliDate instances changes the year for this date.

The Nepali Date API provides some extra useful methods like formatADDate(), toAD() and toBS().

import { formatADDate, toBS, toAD } from '@zener/nepali-datepicker-react';

export default function App() {

  const formattedADDate = formatADDate(new Date(), "dddd, MMMM DD YYYY");

  const adFromBS = toAD(new NepaliDate().toString());

  const bsFromAD = toBS(formatADDate(new Date(), "YYYY-MM-DD"));

  return '...'
}

Following are some extra methods available in Nepali date api.

Method
Return Type
Description
formatADDate(date:Date, format?:string, lang?:ILang)
Formatted string date.
Formats date in given string format pattern. eg. (YYYY/MM/DD), (MMMM DD, YYYY, dddd) and so on.
toAD(date:string)
Object
Returns the year, month and date in AD
toBS(date:string)
Object
Returns the year, month and date in BS