nepali-datetime is a Node.js package designed to provide native JavaScript-like features for Nepali dates. It includes the 'NepaliDate' class for Nepali date support and the 'dateConverter' module for date conversion.
Here are a few examples to get you started:
import NepaliDate from 'nepali-datetime'
// Create a NepaliDate object for the current date and time
const now = new NepaliDate()
console.log(now.toString()) // Outputs: "2080-03-23 15:32:03.643"
// Create a NepaliDate object from a Nepali date string
const date1 = new NepaliDate('2079-02-15 23:11')
console.log(date1.toString()) // Outputs: "2079-02-15 23:11:00"
// Components of a NepaliDate object
const year = now.getYear()
const month = now.getMonth()
const date = now.getDate()
console.log(`Year: ${year}, Month: ${month}, Date: ${date}`) // Outputs: "Year: 2080, Month: 2, Date: 23"
// Format a NepaliDate object
const formattedDate = now.format('YYYY-MM-DD')
console.log(formattedDate) // Outputs: "2080-03-23"
// Create a NepaliDate object from an English date
const date2 = NepaliDate.fromEnglishDate(2023, 6, 8)
console.log(englishDate.toString()) // Outputs: "2080-03-23 00:00:00"
To install nepali-datetime, you can use npm or yarn:
npm install nepali-datetime
or
yarn add nepali-datetime
The NepaliDate
class represents a Nepali calendar date. It provides various methods and properties to work with Nepali dates.
You can create a NepaliDate
object in several ways:
-
Without any parameters: Creates a
NepaliDate
object for the current date and time.import NepaliDate from 'nepali-datetime' const now = new NepaliDate()
-
Using a Nepali date string: Parses the string as a Nepali calendar date.
const date1 = new NepaliDate('2079-02-15') const date2 = new NepaliDate('2079-02-15 14:00')
-
Using a Unix timestamp (in milliseconds):
const date2 = new NepaliDate(1654210800000)
-
Using a JavaScript
Date
object: Converts the JavaScriptDate
object to aNepaliDate
object.const jsDate = new Date() const date3 = new NepaliDate(jsDate)
-
Using an existing
NepaliDate
object: Creates a newNepaliDate
object with the same values.const date4 = new NepaliDate(date3)
-
Using Nepali calendar date and time parameters: Specifies the components of a Nepali calendar date.
const date5 = new NepaliDate(year, month, date, hour, minute, second, ms) const date6 = new NepaliDate(2079, 2, 15, 10, 30)
You can retrieve various components of a NepaliDate
object using the following methods:
getDateObject()
: Retrieves the Javascript Date object equivalent to the NepaliDate.getTime()
: Retrieves the Unix timestamp (in milliseconds) of the Nepali date.getYear()
: Retrieves the year of the Nepali date in the Nepali calendar.getEnglishYear()
: Retrieves the year of the Nepali date in the English calendar.getMonth()
: Retrieves the month of the Nepali date in the Nepali calendar.getEnglishMonth()
: Retrieves the month of the Nepali date in the English calendar.getDate()
: Retrieves the day of the month of the Nepali date in the Nepali calendar.getEnglishDate()
: Retrieves the day of the month of the Nepali date in the English calendar.getDay()
: Retrieves the day of the week represented by a numeric value.getHours()
: Retrieves the hour value of the Nepali date.getMinutes()
: Retrieves the minute value of the Nepali date.getSeconds()
: Retrieves the second value of the Nepali date.getMilliseconds()
: Retrieves the millisecond value of the Nepali date.
You can set individual components of a NepaliDate
object using the following methods:
setYear(year)
: Sets the year of the Nepali date.setMonth(month)
: Sets the month of the Nepali date.setDate(day)
: Sets the day of the month of the Nepali date.setHours(hour)
: Sets the hour of the Nepali date.setMinutes(minute)
: Sets the minute of the Nepali date.setSeconds(second)
: Sets the second of the Nepali date.setMilliseconds(ms)
: Sets the millisecond of the Nepali date.setTime(time)
: Sets the Nepali date and time values using a Unix timestamp.
You can format a NepaliDate
object as a string using the format()
and formatNepali()
methods.
format(formatStr)
: Returns a string representation (in English) of theNepaliDate
object in the specified format.formatNepali(formatStr)
: Returns a string representation in the Nepali (Devanagari) script of theNepaliDate
object in the specified format.
const now = new NepaliDate(2079, 5, 3, 16, 14)
console.log(now.format('YYYY-MM-DD hh:mm A')) // Outputs: 2079-06-03 04:14 PM
The date formatting will follow the format codes mentioned below, which are similar to the date formats used in moment.js.
Format Token | Description | Example |
---|---|---|
YYYY | 4-digit year | 2023 |
YY | 2-digit year | 23 |
MMMM | Full month name | Baishakh |
MMM | Abbreviated month name | Bai |
MM | 2-digit month | 01-12 |
DD | 2-digit day of the month | 01-31 |
dddd | Full day of the week | Monday |
ddd | Abbreviated day of the week | Mon |
HH | 2-digit hour (24-hour format) | 00-23 |
hh | 2-digit hour (12-hour format) | 01-12 |
mm | 2-digit minutes | 00-59 |
ss | 2-digit seconds | 00-59 |
SSS | 3-digit milliseconds | 000-999 |
A | Uppercase AM/PM | AM or PM |
a | Lowercase am/pm | am or pm |
Any other character is printed as is. If you need to print the special characters (YMDymd), enclose them within quotes.
For the list of month names and their abbreviations, you can refer to the table below.
Value of Month | Abbreviation | Full Name |
---|---|---|
0 | Bai | Baishakh |
1 | Jes | Jestha |
2 | Asa | Asar |
3 | Shr | Shrawan |
4 | Bhd | Bhadra |
5 | Asw | Aswin |
6 | Kar | Kartik |
7 | Man | Mangsir |
8 | Pou | Poush |
9 | Mag | Magh |
10 | Fal | Falgun |
11 | Cha | Chaitra |
You can get the equivalent JavaScript Date
object of a NepaliDate
object using the getDateObject()
method.
const now = new NepaliDate(2079, 5, 3)
console.log(now.getDateObject()) // Date 2022-09-18T18:15:00.000Z
You can create a NepaliDate
object from an English calendar date using the fromEnglishDate
method.
const date = NepaliDate.fromEnglishDate(2023, 6, 8)
console.log(date.toString()) // Outputs: "2080-03-23 00:00:00"
The dateConverter
module provides functions for converting dates between the Nepali and English calendars.
englishToNepali(year, month, day)
: Converts an English calendar date to a Nepali calendar date. Returns an array[yearNp, monthNp, dayNp]
representing the Nepali date.nepaliToEnglish(year, month, day)
: Converts a Nepali calendar date to an English calendar date. Returns an array[yearEn, monthEn, dayEn]
representing the English date.
import dateConverter from 'nepali-datetime/dateConverter'
// english to nepali date conversion
const [npYear, npMonth, npDay] = dateConverter.englishToNepali(2023, 5, 27)
// nepali to english date conversion
const [enYear, enMonth, enDay] = dateConverter.nepaliToEnglish(2080, 2, 15)
This project was inspired by nepali-date. We would like to express our gratitude to their team for their excellent work and ideas, which served as a motivation for this project.
We appreciate feedback and contribution to this package. To get started please see our contribution guide.