nepali-date-library 1.1.9 → 1.1.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ReadMe.md +250 -930
- package/dist/index.cjs.js +1 -1
- package/dist/index.mjs.js +1 -1
- package/dist/test.d.ts +1 -0
- package/package.json +3 -4
package/ReadMe.md
CHANGED
|
@@ -1,1042 +1,362 @@
|
|
|
1
1
|
# NepaliDate Library
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
## Features
|
|
6
|
-
|
|
7
|
-
- 🗓️ **Complete Nepali Calendar Support** - Accurate Bikram Sambat calendar from 1976 BS to 2100 BS
|
|
8
|
-
- 🔄 **Flexible Date Conversion** - Convert between AD and BS dates with custom format support
|
|
9
|
-
- 📅 **Date Manipulation** - Add/subtract days, months, and years with proper overflow handling
|
|
10
|
-
- 🌐 **Localization Support** - Full English and Nepali language support for months and days
|
|
11
|
-
- 📊 **Fiscal Year Support** - Built-in fiscal year and quarter calculations
|
|
12
|
-
- 🎯 **Date Range Operations** - Start/end of day, week, month, year calculations
|
|
13
|
-
- ⚡ **TypeScript Ready** - Full type safety and IntelliSense support
|
|
14
|
-
- 🧮 **Calendar Generation** - Generate calendar grids for UI components
|
|
15
|
-
- 🎨 **Custom Format Support** - Parse and format dates in multiple patterns
|
|
3
|
+
## Overview
|
|
4
|
+
The `NepaliDate` library provides a proper way to work with Nepali (Bikram Sambat) dates in TypeScript/JavaScript. It allows you to create, manipulate, format, and convert between Nepali and Gregorian dates with support for fiscal years, quarters, and extensive date operations.
|
|
16
5
|
|
|
17
6
|
## Installation
|
|
18
|
-
|
|
19
|
-
```bash
|
|
7
|
+
```sh
|
|
20
8
|
npm install nepali-date-library
|
|
21
9
|
```
|
|
22
10
|
|
|
23
|
-
##
|
|
24
|
-
|
|
25
|
-
```typescript
|
|
11
|
+
## Importing the Library
|
|
12
|
+
```ts
|
|
26
13
|
import { NepaliDate, ADtoBS, BStoAD } from 'nepali-date-library';
|
|
27
|
-
|
|
28
|
-
// Create today's date in Nepali calendar
|
|
29
|
-
const today = new NepaliDate();
|
|
30
|
-
console.log(today.toString()); // 2081/10/15
|
|
31
|
-
|
|
32
|
-
// Convert between calendars with default formats
|
|
33
|
-
const bsDate = ADtoBS('2025-02-22'); // "2081-10-10"
|
|
34
|
-
const adDate = BStoAD('2081-10-14'); // "2025-02-26"
|
|
35
|
-
|
|
36
|
-
// Format dates
|
|
37
|
-
const formatted = today.format('MMMM DD, YYYY'); // "Magh 15, 2081"
|
|
38
|
-
const nepali = today.format('mmmm dd, yyyy'); // "माघ १५, २०८१"
|
|
39
14
|
```
|
|
40
15
|
|
|
41
|
-
##
|
|
16
|
+
## Class: `NepaliDate`
|
|
42
17
|
|
|
43
|
-
###
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
// Constants
|
|
50
|
-
import {
|
|
51
|
-
NEPALI_DATE_MAP,
|
|
52
|
-
NUMBER_NP,
|
|
53
|
-
WEEK_EN, WEEK_NP, WEEK_SHORT_EN, WEEK_SHORT_NP,
|
|
54
|
-
MONTH_EN, MONTH_NP, MONTH_SHORT_EN, MONTH_SHORT_NP
|
|
55
|
-
} from 'nepali-date-library';
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
### Date Conversion Utilities
|
|
59
|
-
|
|
60
|
-
#### `ADtoBS(adDate: string): string`
|
|
61
|
-
|
|
62
|
-
Converts an Anno Domini (AD) date to a Bikram Sambat (BS) date with custom format support.
|
|
63
|
-
|
|
64
|
-
**Parameters:**
|
|
65
|
-
- `adDate` - The AD date string
|
|
66
|
-
|
|
67
|
-
**Returns:** The corresponding BS date in the specified output format
|
|
68
|
-
|
|
69
|
-
**Supported Input/Output Formats:**
|
|
70
|
-
- `YYYY-MM-DD`, `YYYY/MM/DD`
|
|
71
|
-
|
|
72
|
-
```typescript
|
|
73
|
-
// Default format conversion
|
|
74
|
-
const bsDate = ADtoBS('2025-02-22'); // "2081-10-10"
|
|
18
|
+
### Constructors
|
|
19
|
+
```ts
|
|
20
|
+
new NepaliDate();
|
|
21
|
+
new NepaliDate(date: Date | NepaliDate | number | string);
|
|
22
|
+
new NepaliDate(year: number, month: number, day: number);
|
|
75
23
|
```
|
|
24
|
+
- Creates a `NepaliDate` instance.
|
|
25
|
+
- Accepts either no arguments (current date), a JavaScript `Date`, another `NepaliDate`, a timestamp, or a formatted date string.
|
|
26
|
+
- Can also accept year, month (0-11), and day (1-32) as separate arguments.
|
|
76
27
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
Converts a Bikram Sambat (BS) date to an Anno Domini (AD) date with custom format support.
|
|
28
|
+
### Date Conversion Functions
|
|
80
29
|
|
|
81
|
-
|
|
82
|
-
|
|
30
|
+
#### Pre-built Conversion Functions
|
|
31
|
+
```ts
|
|
32
|
+
import { ADtoBS, BStoAD } from 'nepali-date-library';
|
|
83
33
|
|
|
84
|
-
|
|
34
|
+
// Convert AD to BS
|
|
35
|
+
const bsDate = ADtoBS('2025-02-22'); // Returns: '2081-10-10'
|
|
85
36
|
|
|
86
|
-
|
|
87
|
-
//
|
|
88
|
-
const adDate = BStoAD('2081-10-14'); // "2025-02-26"
|
|
37
|
+
// Convert BS to AD
|
|
38
|
+
const adDate = BStoAD('2081-10-14'); // Returns: '2025-02-26'
|
|
89
39
|
```
|
|
90
40
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
```typescript
|
|
96
|
-
// Current date
|
|
97
|
-
const today = new NepaliDate();
|
|
98
|
-
|
|
99
|
-
// From JavaScript Date
|
|
100
|
-
const fromDate = new NepaliDate(new Date());
|
|
101
|
-
|
|
102
|
-
// From string
|
|
103
|
-
const fromString = new NepaliDate('2081-10-15');
|
|
104
|
-
|
|
105
|
-
// From components (year, month 0-11, day 1-32)
|
|
106
|
-
const fromComponents = new NepaliDate(2081, 9, 15);
|
|
107
|
-
|
|
108
|
-
// From timestamp
|
|
109
|
-
const fromTimestamp = new NepaliDate(1645123200000);
|
|
110
|
-
|
|
111
|
-
// From another NepaliDate
|
|
112
|
-
const copy = new NepaliDate(existingNepaliDate);
|
|
41
|
+
#### Class Method
|
|
42
|
+
```ts
|
|
43
|
+
const nepaliDate = new NepaliDate();
|
|
44
|
+
const adDate = nepaliDate.getEnglishDate(); // Returns JavaScript Date object
|
|
113
45
|
```
|
|
114
46
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
```typescript
|
|
118
|
-
public timestamp: Date; // Equivalent JavaScript Date
|
|
119
|
-
public year: number; // Nepali year (BS)
|
|
120
|
-
public month: number; // Nepali month (0-11)
|
|
121
|
-
public day: number; // Nepali day (1-32)
|
|
122
|
-
```
|
|
47
|
+
---
|
|
123
48
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
date.getSeconds(); // Returns seconds (0-59)
|
|
136
|
-
date.getMilliseconds(); // Returns milliseconds (0-999)
|
|
137
|
-
date.getTime(); // Returns timestamp
|
|
138
|
-
date.getEnglishDate(); // Returns equivalent JavaScript Date
|
|
49
|
+
### Getters
|
|
50
|
+
```ts
|
|
51
|
+
getYear(): number; // Returns Nepali year
|
|
52
|
+
getMonth(): number; // Returns Nepali month (0-11)
|
|
53
|
+
getDate(): number; // Returns Nepali day (1-32)
|
|
54
|
+
getDay(): number; // Returns day of week (0-6, 0 = Sunday)
|
|
55
|
+
getHours(): number; // Returns hour (0-23)
|
|
56
|
+
getMinutes(): number; // Returns minutes (0-59)
|
|
57
|
+
getSeconds(): number; // Returns seconds (0-59)
|
|
58
|
+
getMilliseconds(): number; // Returns milliseconds (0-999)
|
|
59
|
+
getTime(): number; // Returns timestamp in milliseconds
|
|
139
60
|
```
|
|
140
61
|
|
|
141
|
-
|
|
62
|
+
---
|
|
142
63
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
date.set(2081, 9, 15); // Set all components at once
|
|
64
|
+
### Setters
|
|
65
|
+
```ts
|
|
66
|
+
setYear(year: number): void;
|
|
67
|
+
setMonth(month: number): void;
|
|
68
|
+
setDate(day: number): void;
|
|
69
|
+
set(year: number, month: number, day: number): void;
|
|
150
70
|
```
|
|
71
|
+
- Updates the Nepali date components and synchronizes the internal timestamp.
|
|
151
72
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
The `format()` method supports extensive formatting options:
|
|
155
|
-
|
|
156
|
-
```typescript
|
|
157
|
-
const date = new NepaliDate(2081, 9, 15);
|
|
158
|
-
|
|
159
|
-
// English formats
|
|
160
|
-
date.format('YYYY-MM-DD'); // "2081-10-15"
|
|
161
|
-
date.format('MMMM DD, YYYY'); // "Magh 15, 2081"
|
|
162
|
-
date.format('MMM D, YYYY'); // "Mag 15, 2081"
|
|
163
|
-
date.format('DDDD, MMMM DD'); // "Monday, Magh 15"
|
|
73
|
+
---
|
|
164
74
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
date.format('dddd, mmmm dd'); // "सोमबार, माघ १५"
|
|
75
|
+
### Parsing
|
|
76
|
+
```ts
|
|
77
|
+
parse(dateString: string): void;
|
|
169
78
|
```
|
|
79
|
+
- Parses a date string and updates the current instance
|
|
80
|
+
- Supports formats: YYYY-MM-DD, YYYY/MM/DD, or YYYY.MM.DD
|
|
170
81
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
| Token | Description | Example |
|
|
174
|
-
|-------|-------------|---------|
|
|
175
|
-
| `YYYY` | 4-digit year | 2081 |
|
|
176
|
-
| `MM` | Month with leading zero | 01-12 |
|
|
177
|
-
| `M` | Month without leading zero | 1-12 |
|
|
178
|
-
| `MMMM` | Full month name | Baisakh |
|
|
179
|
-
| `MMM` | Short month name | Bai |
|
|
180
|
-
| `DD` | Day with leading zero | 01-32 |
|
|
181
|
-
| `D` | Day without leading zero | 1-32 |
|
|
182
|
-
| `DDDD` | Full day name | Sunday |
|
|
183
|
-
| `DDD` | Short day name | Sun |
|
|
184
|
-
| `yyyy` | 4-digit year (Nepali) | २०८१ |
|
|
185
|
-
| `mm` | Month with leading zero (Nepali) | ०१-१२ |
|
|
186
|
-
| `m` | Month without leading zero (Nepali) | १-१२ |
|
|
187
|
-
| `mmmm` | Full month name (Nepali) | बैशाख |
|
|
188
|
-
| `mmm` | Short month name (Nepali) | बै |
|
|
189
|
-
| `dd` | Day with leading zero (Nepali) | ०१-३२ |
|
|
190
|
-
| `d` | Day without leading zero (Nepali) | १-३२ |
|
|
191
|
-
| `dddd` | Full day name (Nepali) | आइतबार |
|
|
192
|
-
| `ddd` | Short day name (Nepali) | आइत |
|
|
193
|
-
|
|
194
|
-
#### Date Manipulation
|
|
195
|
-
|
|
196
|
-
All manipulation methods return new instances (immutable):
|
|
197
|
-
|
|
198
|
-
```typescript
|
|
199
|
-
const date = new NepaliDate(2081, 9, 15);
|
|
200
|
-
|
|
201
|
-
const futureDate = date.addDays(10); // 10 days later
|
|
202
|
-
const pastDate = date.addMonths(-2); // 2 months earlier
|
|
203
|
-
const nextYear = date.addYears(1); // 1 year later
|
|
82
|
+
---
|
|
204
83
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
84
|
+
### Formatting
|
|
85
|
+
```ts
|
|
86
|
+
format(formatStr: string): string;
|
|
87
|
+
toString(): string; // Returns format: YYYY/MM/DD (1-indexed month)
|
|
208
88
|
```
|
|
209
89
|
|
|
210
|
-
####
|
|
90
|
+
#### Available Format Tokens:
|
|
91
|
+
- **English Formats:**
|
|
92
|
+
- `YYYY` - Full Year (e.g., 2080)
|
|
93
|
+
- `MM` - Month with leading zero (01-12)
|
|
94
|
+
- `M` - Month without leading zero (1-12)
|
|
95
|
+
- `MMM` - Short month name (Bai, Cha)
|
|
96
|
+
- `MMMM` - Long month name (Baisakh, Chaitra)
|
|
97
|
+
- `DD` - Day with leading zero (01-32)
|
|
98
|
+
- `D` - Day without leading zero (1-32)
|
|
99
|
+
- `DDD` - Short day name (Sun, Sat)
|
|
100
|
+
- `DDDD` - Full day name (Sunday)
|
|
211
101
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
102
|
+
- **Nepali Formats:**
|
|
103
|
+
- `yyyy` - Full Year (e.g., २०८१)
|
|
104
|
+
- `mm` - Month with leading zero (०१-१२)
|
|
105
|
+
- `m` - Month without leading zero (१-१२)
|
|
106
|
+
- `mmm` - Short month name (बै, चै)
|
|
107
|
+
- `mmmm` - Long month name (बैशाख, चैत्र)
|
|
108
|
+
- `dd` - Day with leading zero (०१-३२)
|
|
109
|
+
- `d` - Day without leading zero (१-३२)
|
|
110
|
+
- `ddd` - Short day name (आइत, शनि)
|
|
111
|
+
- `dddd` - Full day name (आइतबार)
|
|
215
112
|
|
|
216
|
-
|
|
217
|
-
date1.isBefore(date2); // true
|
|
218
|
-
date1.isEqual(date2); // false
|
|
219
|
-
date1.isSame(date2, 'month'); // true
|
|
220
|
-
date1.isSame(date2, 'year'); // true
|
|
113
|
+
---
|
|
221
114
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
115
|
+
### Date Manipulation
|
|
116
|
+
```ts
|
|
117
|
+
addDays(days: number): NepaliDate;
|
|
118
|
+
addMonths(months: number): NepaliDate;
|
|
119
|
+
addYears(years: number): NepaliDate;
|
|
226
120
|
```
|
|
121
|
+
- Adds a specified number of days, months, or years to the date and returns a new `NepaliDate` instance.
|
|
227
122
|
|
|
228
|
-
|
|
123
|
+
---
|
|
229
124
|
|
|
230
|
-
|
|
231
|
-
const date = new NepaliDate(2081, 5, 15);
|
|
125
|
+
### Date Information Methods
|
|
232
126
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
const endOfDay = date.endOfDay(); // 23:59:59.999
|
|
127
|
+
#### `daysInMonth(): number`
|
|
128
|
+
Returns the number of days in the current month.
|
|
236
129
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
const endOfWeek = date.endOfWeek(); // Following/current Saturday
|
|
130
|
+
#### `isLeapYear(): boolean`
|
|
131
|
+
Checks if the current year is a leap year in the Nepali calendar.
|
|
240
132
|
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
const endOfWeekMon = date.endOfWeek(1); // Following/current Sunday
|
|
133
|
+
#### `getWeeksInMonth(): number`
|
|
134
|
+
Calculates the number of weeks in the current month.
|
|
244
135
|
|
|
245
|
-
|
|
246
|
-
const startOfMonth = date.startOfMonth(); // 1st day of month
|
|
247
|
-
const endOfMonth = date.endOfMonth(); // Last day of month
|
|
136
|
+
---
|
|
248
137
|
|
|
249
|
-
|
|
250
|
-
const startOfYear = date.startOfYear(); // 1st Baisakh
|
|
251
|
-
const endOfYear = date.endOfYear(); // Last day of Chaitra
|
|
252
|
-
```
|
|
138
|
+
### Date Comparison Methods
|
|
253
139
|
|
|
254
|
-
####
|
|
140
|
+
#### `diff(date: NepaliDate, unit: 'year' | 'month' | 'day'): number`
|
|
141
|
+
Calculates the difference between two `NepaliDate` instances.
|
|
255
142
|
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
const currentQuarter = new NepaliDate().getCurrentQuarter();
|
|
143
|
+
#### `isAfter(date: NepaliDate): boolean`
|
|
144
|
+
Checks if this date comes after the specified date.
|
|
259
145
|
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
console.log(q1.start.toString()); // "2081/1/1" (1st Baisakh)
|
|
263
|
-
console.log(q1.end.toString()); // "2081/3/32" (Last day of Asar)
|
|
146
|
+
#### `isBefore(date: NepaliDate): boolean`
|
|
147
|
+
Checks if this date comes before the specified date.
|
|
264
148
|
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
console.log(quarters.Q1); // {start: NepaliDate, end: NepaliDate}
|
|
268
|
-
console.log(quarters.Q2); // {start: NepaliDate, end: NepaliDate}
|
|
269
|
-
console.log(quarters.Q3); // {start: NepaliDate, end: NepaliDate}
|
|
270
|
-
console.log(quarters.Q4); // {start: NepaliDate, end: NepaliDate}
|
|
271
|
-
```
|
|
149
|
+
#### `isEqual(date: NepaliDate): boolean`
|
|
150
|
+
Checks if this date is exactly equal to the specified date (year, month, day).
|
|
272
151
|
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
- **Q2:** Shrawan, Bhadra, Aswin (months 3-5)
|
|
276
|
-
- **Q3:** Kartik, Mangsir, Poush (months 6-8)
|
|
277
|
-
- **Q4:** Magh, Falgun, Chaitra (months 9-11)
|
|
152
|
+
#### `isSame(date: NepaliDate, unit: 'year' | 'month' | 'day'): boolean`
|
|
153
|
+
Checks if this date is the same as the specified date for the given unit.
|
|
278
154
|
|
|
279
|
-
|
|
155
|
+
---
|
|
280
156
|
|
|
281
|
-
|
|
157
|
+
### Date Range Methods
|
|
282
158
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
const currentFY = NepaliDate.getCurrentFiscalYear();
|
|
159
|
+
#### `startOfDay(): NepaliDate`
|
|
160
|
+
Returns a new `NepaliDate` representing the start of the current day (00:00:00).
|
|
286
161
|
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
const fyQuarter = date.getCurrentFiscalYearQuarter();
|
|
162
|
+
#### `endOfDay(): NepaliDate`
|
|
163
|
+
Returns a new `NepaliDate` representing the end of the current day (23:59:59.999).
|
|
290
164
|
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
console.log(quarterDates.end.toString());
|
|
165
|
+
#### `startOfWeek(startOfWeek: number = 0): NepaliDate`
|
|
166
|
+
Returns a new `NepaliDate` representing the start of the week containing this date.
|
|
167
|
+
- `startOfWeek`: Day to consider as start of week (0-6, where 0 = Sunday, 1 = Monday, etc.)
|
|
295
168
|
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
console.log(fyQ1.start.toString()); // "2081/4/1" (1st Shrawan)
|
|
299
|
-
console.log(fyQ1.end.toString()); // "2081/6/30" (Last day of Aswin)
|
|
169
|
+
#### `endOfWeek(startOfWeek: number = 0): NepaliDate`
|
|
170
|
+
Returns a new `NepaliDate` representing the end of the week containing this date.
|
|
300
171
|
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
```
|
|
172
|
+
#### `startOfMonth(): NepaliDate`
|
|
173
|
+
Returns a new `NepaliDate` representing the first day of the current month.
|
|
304
174
|
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
- **Q2:** Kartik, Mangsir, Poush (months 6-8)
|
|
308
|
-
- **Q3:** Magh, Falgun, Chaitra (months 9-11)
|
|
309
|
-
- **Q4:** Baisakh, Jestha, Asar (months 0-2) *[spans to next calendar year]*
|
|
175
|
+
#### `endOfMonth(): NepaliDate`
|
|
176
|
+
Returns a new `NepaliDate` representing the last day of the current month.
|
|
310
177
|
|
|
311
|
-
####
|
|
178
|
+
#### `startOfYear(): NepaliDate`
|
|
179
|
+
Returns a new `NepaliDate` representing the first day of the current Nepali year (1st Baisakh).
|
|
312
180
|
|
|
313
|
-
|
|
314
|
-
|
|
181
|
+
#### `endOfYear(): NepaliDate`
|
|
182
|
+
Returns a new `NepaliDate` representing the last day of the current Nepali year (last day of Chaitra).
|
|
315
183
|
|
|
316
|
-
|
|
317
|
-
date.isLeapYear(); // true/false
|
|
318
|
-
date.getWeeksInMonth(); // Number of weeks in current month
|
|
319
|
-
date.isValid(); // true/false
|
|
184
|
+
---
|
|
320
185
|
|
|
321
|
-
|
|
322
|
-
NepaliDate.isValid(2081, 4, 32); // false (invalid day)
|
|
323
|
-
NepaliDate.isValid(2081, 4, 31); // true
|
|
324
|
-
```
|
|
186
|
+
### Quarter and Fiscal Year Methods
|
|
325
187
|
|
|
326
|
-
####
|
|
188
|
+
#### `getCurrentQuarter(): number`
|
|
189
|
+
Returns the quarter number (1-4) for the current date.
|
|
327
190
|
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
const calendarData = NepaliDate.getCalendarDays(2081, 4); // Bhadra 2081
|
|
332
|
-
|
|
333
|
-
console.log(calendarData);
|
|
334
|
-
// {
|
|
335
|
-
// prevRemainingDays: 2, // Days from previous month
|
|
336
|
-
// prevMonth: {
|
|
337
|
-
// year: 2081,
|
|
338
|
-
// month: 3,
|
|
339
|
-
// days: [30, 31] // Last days of previous month
|
|
340
|
-
// },
|
|
341
|
-
// currentMonth: {
|
|
342
|
-
// year: 2081,
|
|
343
|
-
// month: 4,
|
|
344
|
-
// days: [1, 2, 3, ..., 31] // All days of current month
|
|
345
|
-
// },
|
|
346
|
-
// nextMonth: {
|
|
347
|
-
// year: 2081,
|
|
348
|
-
// month: 5,
|
|
349
|
-
// days: [1, 2, 3, 4, 5] // First days of next month
|
|
350
|
-
// },
|
|
351
|
-
// remainingDays: 5 // Days from next month
|
|
352
|
-
// }
|
|
353
|
-
```
|
|
191
|
+
#### `getCurrentFiscalYearQuarter(): number`
|
|
192
|
+
Returns the current fiscal year quarter number (1-4) for the current date.
|
|
193
|
+
- Fiscal year starts from Shrawan 1st (month 3, day 1)
|
|
354
194
|
|
|
355
|
-
####
|
|
195
|
+
#### `getCurrentFiscalYearQuarterDates(): { start: NepaliDate; end: NepaliDate }`
|
|
196
|
+
Returns the start and end dates of the current fiscal year quarter.
|
|
356
197
|
|
|
357
|
-
|
|
358
|
-
// Get name utilities
|
|
359
|
-
NepaliDate.getMonthName(0, false, false); // "Baisakh"
|
|
360
|
-
NepaliDate.getMonthName(0, true, false); // "Bai"
|
|
361
|
-
NepaliDate.getMonthName(0, false, true); // "बैशाख"
|
|
362
|
-
NepaliDate.getMonthName(0, true, true); // "बै"
|
|
198
|
+
---
|
|
363
199
|
|
|
364
|
-
|
|
365
|
-
NepaliDate.getDayName(0, true, false); // "Sun"
|
|
366
|
-
NepaliDate.getDayName(0, false, true); // "आइतबार"
|
|
367
|
-
NepaliDate.getDayName(0, true, true); // "आइत"
|
|
200
|
+
### Static Methods
|
|
368
201
|
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
// Clone instance
|
|
374
|
-
const copy = date.clone(); // Deep copy of NepaliDate
|
|
375
|
-
```
|
|
376
|
-
|
|
377
|
-
### Constants
|
|
378
|
-
|
|
379
|
-
#### Calendar Data
|
|
380
|
-
|
|
381
|
-
```typescript
|
|
382
|
-
// Complete calendar mapping (1976-2100 BS)
|
|
383
|
-
NEPALI_DATE_MAP: Array<{
|
|
384
|
-
year: number;
|
|
385
|
-
days: number[]; // Days in each month [31, 32, 31, ...]
|
|
386
|
-
totalDays: number; // Total days in year
|
|
387
|
-
daysTillNow: number; // Cumulative days from epoch
|
|
388
|
-
}>
|
|
202
|
+
#### Date Range Utilities
|
|
203
|
+
```ts
|
|
204
|
+
static minimum(): Date; // Returns earliest supported date
|
|
205
|
+
static maximum(): Date; // Returns latest supported date
|
|
389
206
|
```
|
|
390
207
|
|
|
391
|
-
####
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
//
|
|
395
|
-
WEEK_EN: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
|
|
396
|
-
WEEK_SHORT_EN: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
|
|
397
|
-
|
|
398
|
-
// Nepali
|
|
399
|
-
WEEK_NP: ['आइतबार', 'सोमबार', 'मंगलबार', 'बुधबार', 'बिहिबार', 'शुक्रबार', 'शनिबार']
|
|
400
|
-
WEEK_SHORT_NP: ['आइत', 'सोम', 'मंगल', 'बुध', 'बिहि', 'शुक्र', 'शनि']
|
|
208
|
+
#### Validation
|
|
209
|
+
```ts
|
|
210
|
+
static isValid(year: number, month: number, day: number): boolean;
|
|
211
|
+
isValid(): boolean; // Instance method
|
|
401
212
|
```
|
|
402
213
|
|
|
403
|
-
####
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
MONTH_EN: ["Baisakh", "Jestha", "Asar", "Shrawan", "Bhadra", "Aswin", "Kartik", "Mangsir", "Poush", "Magh", "Falgun", "Chaitra"]
|
|
408
|
-
MONTH_SHORT_EN: ["Bai", "Jes", "Asa", "Shr", "Bhd", "Asw", "Kar", "Man", "Pou", "Mag", "Fal", "Cha"]
|
|
409
|
-
|
|
410
|
-
// Nepali
|
|
411
|
-
MONTH_NP: ['बैशाख', 'जेठ', 'असार', 'श्रावण', 'भाद्र', 'आश्विन', 'कार्तिक', 'मंसिर', 'पौष', 'माघ', 'फाल्गुण', 'चैत्र']
|
|
412
|
-
MONTH_SHORT_NP: ['बै', 'जे', 'अ', 'श्रा', 'भा', 'आ', 'का', 'मं', 'पौ', 'मा', 'फा', 'चै']
|
|
214
|
+
#### Name Utilities
|
|
215
|
+
```ts
|
|
216
|
+
static getMonthName(month: number, short: boolean = false, nepali: boolean = false): string;
|
|
217
|
+
static getDayName(day: number, short: boolean = false, nepali: boolean = false): string;
|
|
413
218
|
```
|
|
414
219
|
|
|
415
|
-
####
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
return num.split('').map(digit => NUMBER_NP[parseInt(digit)] || digit).join('');
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
console.log(toNepaliNumber('2081')); // "२०८१"
|
|
427
|
-
```
|
|
428
|
-
|
|
429
|
-
## Examples
|
|
430
|
-
|
|
431
|
-
### Basic Date Operations
|
|
432
|
-
|
|
433
|
-
```typescript
|
|
434
|
-
import { NepaliDate } from 'nepali-date-library';
|
|
435
|
-
|
|
436
|
-
const date = new NepaliDate(2081, 9, 15); // Magh 15, 2081
|
|
437
|
-
|
|
438
|
-
// Formatting
|
|
439
|
-
console.log(date.toString()); // "2081/10/15"
|
|
440
|
-
console.log(date.format('YYYY-MM-DD')); // "2081-10-15"
|
|
441
|
-
console.log(date.format('MMMM DD, YYYY')); // "Magh 15, 2081"
|
|
442
|
-
console.log(date.format('dddd, mmmm dd, yyyy')); // "सोमबार, माघ १५, २०८१"
|
|
443
|
-
|
|
444
|
-
// Date arithmetic
|
|
445
|
-
const nextWeek = date.addDays(7);
|
|
446
|
-
const nextMonth = date.addMonths(1);
|
|
447
|
-
const nextYear = date.addYears(1);
|
|
448
|
-
|
|
449
|
-
console.log(nextWeek.format('YYYY-MM-DD')); // "2081-10-22"
|
|
450
|
-
console.log(nextMonth.format('YYYY-MM-DD')); // "2081-11-15"
|
|
451
|
-
console.log(nextYear.format('YYYY-MM-DD')); // "2082-10-15"
|
|
452
|
-
```
|
|
453
|
-
|
|
454
|
-
### Working with Localized Names
|
|
455
|
-
|
|
456
|
-
```typescript
|
|
457
|
-
import { MONTH_EN, MONTH_NP, WEEK_EN, WEEK_NP, NUMBER_NP } from 'nepali-date-library';
|
|
458
|
-
|
|
459
|
-
const date = new NepaliDate();
|
|
460
|
-
|
|
461
|
-
// Get localized month and day names
|
|
462
|
-
const monthNameEn = MONTH_EN[date.getMonth()];
|
|
463
|
-
const monthNameNp = MONTH_NP[date.getMonth()];
|
|
464
|
-
const dayNameEn = WEEK_EN[date.getDay()];
|
|
465
|
-
const dayNameNp = WEEK_NP[date.getDay()];
|
|
466
|
-
|
|
467
|
-
// Convert to Nepali numerals
|
|
468
|
-
const nepaliYear = date.getYear().toString()
|
|
469
|
-
.split('')
|
|
470
|
-
.map(digit => NUMBER_NP[parseInt(digit)])
|
|
471
|
-
.join('');
|
|
472
|
-
|
|
473
|
-
console.log(`${dayNameEn}, ${monthNameEn} ${date.getDate()}, ${date.getYear()}`);
|
|
474
|
-
// "Monday, Magh 15, 2081"
|
|
475
|
-
|
|
476
|
-
console.log(`${dayNameNp}, ${monthNameNp} ${NUMBER_NP[1]}${NUMBER_NP[5]}, ${nepaliYear}`);
|
|
477
|
-
// "सोमबार, माघ १५, २०८१"
|
|
478
|
-
```
|
|
479
|
-
|
|
480
|
-
### Fiscal Year Operations
|
|
481
|
-
|
|
482
|
-
```typescript
|
|
483
|
-
import { NepaliDate } from 'nepali-date-library';
|
|
484
|
-
|
|
485
|
-
// Current fiscal year info
|
|
486
|
-
const currentFY = NepaliDate.getCurrentFiscalYear();
|
|
487
|
-
console.log(`Current Fiscal Year: ${currentFY}`);
|
|
488
|
-
|
|
489
|
-
const date = new NepaliDate();
|
|
490
|
-
const currentFYQuarter = date.getCurrentFiscalYearQuarter();
|
|
491
|
-
console.log(`Current FY Quarter: Q${currentFYQuarter}`);
|
|
492
|
-
|
|
493
|
-
// Get all fiscal year quarters
|
|
494
|
-
const fyQuarters = NepaliDate.getFiscalYearQuarters(2081);
|
|
495
|
-
|
|
496
|
-
Object.entries(fyQuarters).forEach(([quarter, dates]) => {
|
|
497
|
-
console.log(`${quarter}: ${dates.start.format('YYYY-MM-DD')} to ${dates.end.format('YYYY-MM-DD')}`);
|
|
498
|
-
});
|
|
499
|
-
|
|
500
|
-
// Output:
|
|
501
|
-
// Q1: 2081-04-01 to 2081-06-30 (Shrawan to Aswin)
|
|
502
|
-
// Q2: 2081-07-01 to 2081-09-30 (Kartik to Poush)
|
|
503
|
-
// Q3: 2081-10-01 to 2081-12-30 (Magh to Chaitra)
|
|
504
|
-
// Q4: 2082-01-01 to 2082-03-32 (Baisakh to Asar)
|
|
505
|
-
```
|
|
506
|
-
|
|
507
|
-
### Building a Calendar Component
|
|
508
|
-
|
|
509
|
-
```typescript
|
|
510
|
-
import { NepaliDate, WEEK_EN, MONTH_EN } from 'nepali-date-library';
|
|
511
|
-
|
|
512
|
-
function generateCalendar(year: number, month: number) {
|
|
513
|
-
const calendarData = NepaliDate.getCalendarDays(year, month);
|
|
514
|
-
|
|
515
|
-
// Create 6-week calendar grid
|
|
516
|
-
const calendar = [];
|
|
517
|
-
let dayIndex = 0;
|
|
518
|
-
|
|
519
|
-
for (let week = 0; week < 6; week++) {
|
|
520
|
-
const weekDays = [];
|
|
521
|
-
|
|
522
|
-
for (let day = 0; day < 7; day++) {
|
|
523
|
-
if (week === 0 && day < calendarData.prevRemainingDays) {
|
|
524
|
-
// Previous month days
|
|
525
|
-
weekDays.push({
|
|
526
|
-
day: calendarData.prevMonth.days[day],
|
|
527
|
-
isCurrentMonth: false,
|
|
528
|
-
isPrevMonth: true,
|
|
529
|
-
date: new NepaliDate(
|
|
530
|
-
calendarData.prevMonth.year,
|
|
531
|
-
calendarData.prevMonth.month,
|
|
532
|
-
calendarData.prevMonth.days[day]
|
|
533
|
-
)
|
|
534
|
-
});
|
|
535
|
-
} else if (dayIndex < calendarData.currentMonth.days.length) {
|
|
536
|
-
// Current month days
|
|
537
|
-
weekDays.push({
|
|
538
|
-
day: calendarData.currentMonth.days[dayIndex],
|
|
539
|
-
isCurrentMonth: true,
|
|
540
|
-
isPrevMonth: false,
|
|
541
|
-
date: new NepaliDate(year, month, calendarData.currentMonth.days[dayIndex])
|
|
542
|
-
});
|
|
543
|
-
dayIndex++;
|
|
544
|
-
} else {
|
|
545
|
-
// Next month days
|
|
546
|
-
const nextDayIndex = dayIndex - calendarData.currentMonth.days.length;
|
|
547
|
-
weekDays.push({
|
|
548
|
-
day: calendarData.nextMonth.days[nextDayIndex],
|
|
549
|
-
isCurrentMonth: false,
|
|
550
|
-
isPrevMonth: false,
|
|
551
|
-
date: new NepaliDate(
|
|
552
|
-
calendarData.nextMonth.year,
|
|
553
|
-
calendarData.nextMonth.month,
|
|
554
|
-
calendarData.nextMonth.days[nextDayIndex]
|
|
555
|
-
)
|
|
556
|
-
});
|
|
557
|
-
}
|
|
558
|
-
}
|
|
559
|
-
calendar.push(weekDays);
|
|
560
|
-
}
|
|
561
|
-
|
|
562
|
-
return {
|
|
563
|
-
month: MONTH_EN[month],
|
|
564
|
-
year: year,
|
|
565
|
-
weeks: calendar,
|
|
566
|
-
weekHeaders: WEEK_EN
|
|
567
|
-
};
|
|
568
|
-
}
|
|
569
|
-
|
|
570
|
-
// Usage
|
|
571
|
-
const calendar = generateCalendar(2081, 9); // Magh 2081
|
|
572
|
-
console.log(calendar.month); // "Magh"
|
|
573
|
-
console.log(calendar.weeks); // 6 weeks of calendar data
|
|
574
|
-
```
|
|
575
|
-
|
|
576
|
-
### Date Conversion Workflow
|
|
577
|
-
|
|
578
|
-
```typescript
|
|
579
|
-
import { NepaliDate, ADtoBS, BStoAD } from 'nepali-date-library';
|
|
580
|
-
|
|
581
|
-
// Convert user input
|
|
582
|
-
function handleDateConversion(inputDate: string, fromCalendar: 'AD' | 'BS') {
|
|
583
|
-
try {
|
|
584
|
-
if (fromCalendar === 'AD') {
|
|
585
|
-
const bsDate = ADtoBS(inputDate);
|
|
586
|
-
const nepaliDate = new NepaliDate(bsDate);
|
|
587
|
-
|
|
588
|
-
return {
|
|
589
|
-
converted: bsDate,
|
|
590
|
-
formatted: nepaliDate.format('MMMM DD, YYYY'),
|
|
591
|
-
nepaliFormatted: nepaliDate.format('mmmm dd, yyyy'),
|
|
592
|
-
dayName: nepaliDate.format('DDDD')
|
|
593
|
-
};
|
|
594
|
-
} else {
|
|
595
|
-
const adDate = BStoAD(inputDate);
|
|
596
|
-
const jsDate = new Date(adDate);
|
|
597
|
-
|
|
598
|
-
return {
|
|
599
|
-
converted: adDate,
|
|
600
|
-
formatted: jsDate.toLocaleDateString('en-US', {
|
|
601
|
-
year: 'numeric',
|
|
602
|
-
month: 'long',
|
|
603
|
-
day: 'numeric'
|
|
604
|
-
})
|
|
605
|
-
};
|
|
606
|
-
}
|
|
607
|
-
} catch (error) {
|
|
608
|
-
throw new Error(`Invalid date: ${error.message}`);
|
|
609
|
-
}
|
|
220
|
+
#### Calendar Generation
|
|
221
|
+
```ts
|
|
222
|
+
static getCalendarDays(year: number, month: number): {
|
|
223
|
+
prevRemainingDays: number,
|
|
224
|
+
prevMonth: { year: number, month: number, days: number[] },
|
|
225
|
+
currentMonth: { year: number, month: number, days: number[] },
|
|
226
|
+
nextMonth: { year: number, month: number, days: number[] },
|
|
227
|
+
remainingDays: number
|
|
610
228
|
}
|
|
611
|
-
|
|
612
|
-
// Examples
|
|
613
|
-
console.log(handleDateConversion('2025-02-22', 'AD'));
|
|
614
|
-
// { converted: "2081-10-10", formatted: "Magh 10, 2081", ... }
|
|
615
|
-
|
|
616
|
-
console.log(handleDateConversion('2081-10-14', 'BS'));
|
|
617
|
-
// { converted: "2025-02-26", formatted: "February 26, 2025" }
|
|
618
229
|
```
|
|
230
|
+
Generates calendar days for a given month, including trailing/leading days from adjacent months.
|
|
619
231
|
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
const endOfMonth = startOfMonth.endOfMonth();
|
|
630
|
-
|
|
631
|
-
let current = startOfMonth.clone();
|
|
632
|
-
|
|
633
|
-
// Find first Friday
|
|
634
|
-
while (current.getDay() !== 5) { // 5 = Friday
|
|
635
|
-
current = current.addDays(1);
|
|
636
|
-
}
|
|
637
|
-
|
|
638
|
-
// Collect all Fridays
|
|
639
|
-
while (current.isBefore(endOfMonth) || current.isEqual(endOfMonth)) {
|
|
640
|
-
fridays.push(current.clone());
|
|
641
|
-
current = current.addDays(7);
|
|
642
|
-
}
|
|
643
|
-
|
|
644
|
-
return fridays;
|
|
645
|
-
}
|
|
646
|
-
|
|
647
|
-
// Usage
|
|
648
|
-
const fridaysInMagh = getFridaysInMonth(2081, 9);
|
|
649
|
-
fridaysInMagh.forEach(friday => {
|
|
650
|
-
console.log(friday.format('DDDD, MMMM DD')); // "Friday, Magh 3", etc.
|
|
651
|
-
});
|
|
232
|
+
#### Quarter Methods
|
|
233
|
+
```ts
|
|
234
|
+
static getQuarter(quarter: number, year?: number): { start: NepaliDate; end: NepaliDate };
|
|
235
|
+
static getQuarters(year?: number): {
|
|
236
|
+
Q1: { start: NepaliDate; end: NepaliDate };
|
|
237
|
+
Q2: { start: NepaliDate; end: NepaliDate };
|
|
238
|
+
Q3: { start: NepaliDate; end: NepaliDate };
|
|
239
|
+
Q4: { start: NepaliDate; end: NepaliDate };
|
|
240
|
+
};
|
|
652
241
|
```
|
|
653
242
|
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
const invalid2 = new NepaliDate(1975, 0, 1); // Throws: Year out of range
|
|
665
|
-
|
|
666
|
-
// Invalid day for month
|
|
667
|
-
const invalid3 = new NepaliDate(2081, 4, 32); // Throws: Invalid date
|
|
668
|
-
|
|
669
|
-
// Invalid quarter
|
|
670
|
-
const invalid4 = NepaliDate.getQuarter(5, 2081); // Throws: Quarter must be 1-4
|
|
671
|
-
|
|
672
|
-
} catch (error) {
|
|
673
|
-
console.error(error.message);
|
|
674
|
-
}
|
|
675
|
-
|
|
676
|
-
// Safe validation
|
|
677
|
-
if (NepaliDate.isValid(2081, 9, 15)) {
|
|
678
|
-
const safeDate = new NepaliDate(2081, 9, 15);
|
|
679
|
-
console.log('Date created successfully');
|
|
680
|
-
}
|
|
243
|
+
#### Fiscal Year Methods
|
|
244
|
+
```ts
|
|
245
|
+
static getCurrentFiscalYear(): number;
|
|
246
|
+
static getFiscalYearQuarter(quarter: number, fiscalYear?: number): { start: NepaliDate; end: NepaliDate };
|
|
247
|
+
static getFiscalYearQuarters(fiscalYear?: number): {
|
|
248
|
+
Q1: { start: NepaliDate; end: NepaliDate };
|
|
249
|
+
Q2: { start: NepaliDate; end: NepaliDate };
|
|
250
|
+
Q3: { start: NepaliDate; end: NepaliDate };
|
|
251
|
+
Q4: { start: NepaliDate; end: NepaliDate };
|
|
252
|
+
};
|
|
681
253
|
```
|
|
682
254
|
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
- **End:** 2100 BS (Bikram Sambat year 2100)
|
|
687
|
-
- **Total:** 125 years of accurate calendar data
|
|
688
|
-
|
|
689
|
-
```typescript
|
|
690
|
-
const minDate = NepaliDate.minimum(); // JavaScript Date for 1976/1/1 BS
|
|
691
|
-
const maxDate = NepaliDate.maximum(); // JavaScript Date for 2100/12/31 BS
|
|
255
|
+
#### Utility Methods
|
|
256
|
+
```ts
|
|
257
|
+
clone(): NepaliDate; // Creates a copy of the current instance
|
|
692
258
|
```
|
|
693
259
|
|
|
694
|
-
|
|
260
|
+
---
|
|
695
261
|
|
|
696
|
-
|
|
697
|
-
- **Internal storage and input:** Months are 0-indexed (0-11) like JavaScript Date
|
|
698
|
-
- **Display:** `toString()` method shows months as 1-12 for readability
|
|
699
|
-
- **Baisakh = 0, Jestha = 1, ..., Chaitra = 11**
|
|
262
|
+
## Example Usage
|
|
700
263
|
|
|
701
|
-
###
|
|
702
|
-
|
|
264
|
+
### Basic Usage
|
|
265
|
+
```ts
|
|
266
|
+
const today = new NepaliDate();
|
|
267
|
+
console.log(today.toString()); // 2081/10/15
|
|
703
268
|
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
const modified = original.addDays(10);
|
|
269
|
+
const formatted = today.format('YYYY-MM-DD');
|
|
270
|
+
console.log(formatted); // 2081-10-15
|
|
707
271
|
|
|
708
|
-
|
|
709
|
-
console.log(
|
|
272
|
+
const nepaliFormatted = today.format('yyyy-mm-dd');
|
|
273
|
+
console.log(nepaliFormatted); // २०८१-१०-१५
|
|
710
274
|
```
|
|
711
275
|
|
|
712
|
-
###
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
```typescript
|
|
720
|
-
// Instead of: nepaliDate.getBS()
|
|
721
|
-
const nepaliDate = new NepaliDate();
|
|
722
|
-
console.log(nepaliDate.toString());
|
|
723
|
-
|
|
724
|
-
// Instead of: nepaliDate.getAD()
|
|
725
|
-
console.log(nepaliDate.getEnglishDate());
|
|
726
|
-
|
|
727
|
-
// Instead of: nepaliDate.format('YYYY-MM-DD')
|
|
728
|
-
console.log(nepaliDate.format('YYYY-MM-DD'));
|
|
276
|
+
### Date Manipulation
|
|
277
|
+
```ts
|
|
278
|
+
const date = new NepaliDate(2081, 9, 15);
|
|
279
|
+
const futureDate = date.addDays(10);
|
|
280
|
+
const pastDate = date.addMonths(-2);
|
|
281
|
+
const nextYear = date.addYears(1);
|
|
729
282
|
```
|
|
730
283
|
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
```typescript
|
|
736
|
-
import { NepaliDate } from 'nepali-date-library';
|
|
284
|
+
### Date Comparison
|
|
285
|
+
```ts
|
|
286
|
+
const date1 = new NepaliDate(2081, 5, 10);
|
|
287
|
+
const date2 = new NepaliDate(2081, 5, 15);
|
|
737
288
|
|
|
738
|
-
//
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
const formatted: string = date.format('YYYY-MM-DD'); // Type-safe
|
|
289
|
+
console.log(date1.isBefore(date2)); // true
|
|
290
|
+
console.log(date1.diff(date2, 'day')); // -5
|
|
291
|
+
console.log(date1.isSame(date2, 'month')); // true
|
|
742
292
|
```
|
|
743
293
|
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
- **Dependencies:** None (zero external dependencies)
|
|
749
|
-
|
|
750
|
-
## Contributing
|
|
751
|
-
|
|
752
|
-
We welcome contributions! Please feel free to submit issues and enhancement requests.
|
|
753
|
-
|
|
754
|
-
## License
|
|
755
|
-
|
|
756
|
-
MIT License - see LICENSE file for details.
|
|
757
|
-
|
|
758
|
-
## Acknowledgments
|
|
759
|
-
|
|
760
|
-
This project was inspired by [nepali-date](https://github.com/sharingapples/nepali-date) and provides enhanced functionality with TypeScript support and additional features.
|
|
761
|
-
|
|
762
|
-
## Performance Considerations
|
|
763
|
-
|
|
764
|
-
### Memory Usage
|
|
765
|
-
- Minimal memory footprint with efficient date calculations
|
|
766
|
-
- Calendar data is pre-computed and stored in constants
|
|
767
|
-
- No unnecessary object creation in core operations
|
|
768
|
-
|
|
769
|
-
### Computation Speed
|
|
770
|
-
- Fast date conversions using pre-calculated lookup tables
|
|
771
|
-
- O(1) complexity for most operations
|
|
772
|
-
- Optimized algorithms for date arithmetic
|
|
294
|
+
### Working with Quarters
|
|
295
|
+
```ts
|
|
296
|
+
// Get current quarter
|
|
297
|
+
const currentQuarter = new NepaliDate().getCurrentQuarter();
|
|
773
298
|
|
|
774
|
-
|
|
299
|
+
// Get fiscal year quarter
|
|
300
|
+
const fiscalQuarter = new NepaliDate().getCurrentFiscalYearQuarter();
|
|
775
301
|
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
//
|
|
779
|
-
function calculateDeliveryDate(orderDate: string, deliveryDays: number) {
|
|
780
|
-
const bsOrderDate = new NepaliDate(ADtoBS(orderDate));
|
|
781
|
-
const deliveryDate = bsOrderDate.addDays(deliveryDays);
|
|
782
|
-
|
|
783
|
-
return {
|
|
784
|
-
nepali: deliveryDate.format('MMMM DD, YYYY'),
|
|
785
|
-
english: deliveryDate.format('mmmm dd, yyyy'),
|
|
786
|
-
dayOfWeek: deliveryDate.format('DDDD')
|
|
787
|
-
};
|
|
788
|
-
}
|
|
789
|
-
|
|
790
|
-
const delivery = calculateDeliveryDate('2025-02-20', 5);
|
|
791
|
-
console.log(delivery);
|
|
792
|
-
// { nepali: "Magh 13, 2081", english: "माघ १३, २०८१", dayOfWeek: "Wednesday" }
|
|
793
|
-
```
|
|
794
|
-
|
|
795
|
-
### Financial Applications
|
|
796
|
-
```typescript
|
|
797
|
-
// Calculate fiscal year quarters for reporting
|
|
798
|
-
function getFiscalYearReport(fiscalYear: number) {
|
|
799
|
-
const quarters = NepaliDate.getFiscalYearQuarters(fiscalYear);
|
|
800
|
-
|
|
801
|
-
return Object.entries(quarters).map(([quarter, dates]) => ({
|
|
802
|
-
quarter,
|
|
803
|
-
startDate: dates.start.format('YYYY-MM-DD'),
|
|
804
|
-
endDate: dates.end.format('YYYY-MM-DD'),
|
|
805
|
-
daysInQuarter: dates.start.diff(dates.end, 'day') * -1 + 1
|
|
806
|
-
}));
|
|
807
|
-
}
|
|
302
|
+
// Get all quarters for a year
|
|
303
|
+
const quarters = NepaliDate.getQuarters(2081);
|
|
304
|
+
console.log(quarters.Q1); // { start: NepaliDate, end: NepaliDate }
|
|
808
305
|
|
|
809
|
-
|
|
810
|
-
|
|
306
|
+
// Get fiscal year quarters
|
|
307
|
+
const fiscalQuarters = NepaliDate.getFiscalYearQuarters(2081);
|
|
811
308
|
```
|
|
812
309
|
|
|
813
|
-
###
|
|
814
|
-
```
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
while (current.getDay() !== targetDay) {
|
|
820
|
-
current = current.addDays(1);
|
|
821
|
-
}
|
|
822
|
-
|
|
823
|
-
return current;
|
|
824
|
-
}
|
|
825
|
-
|
|
826
|
-
// Find all Saturdays in a month for scheduling
|
|
827
|
-
function getSaturdaysInMonth(year: number, month: number): NepaliDate[] {
|
|
828
|
-
const saturdays: NepaliDate[] = [];
|
|
829
|
-
const monthStart = new NepaliDate(year, month, 1);
|
|
830
|
-
const monthEnd = monthStart.endOfMonth();
|
|
831
|
-
|
|
832
|
-
let current = monthStart.clone();
|
|
833
|
-
|
|
834
|
-
// Find first Saturday
|
|
835
|
-
while (current.getDay() !== 6) {
|
|
836
|
-
current = current.addDays(1);
|
|
837
|
-
}
|
|
838
|
-
|
|
839
|
-
// Collect all Saturdays
|
|
840
|
-
while (current.getDate() <= monthEnd.getDate() && current.getMonth() === month) {
|
|
841
|
-
saturdays.push(current.clone());
|
|
842
|
-
current = current.addDays(7);
|
|
843
|
-
}
|
|
844
|
-
|
|
845
|
-
return saturdays;
|
|
846
|
-
}
|
|
310
|
+
### Calendar Generation
|
|
311
|
+
```ts
|
|
312
|
+
const calendarData = NepaliDate.getCalendarDays(2081, 4);
|
|
313
|
+
console.log(calendarData.currentMonth.days); // [1, 2, 3, ..., 30]
|
|
314
|
+
console.log(calendarData.prevMonth.days); // Days from previous month
|
|
847
315
|
```
|
|
848
316
|
|
|
849
|
-
###
|
|
850
|
-
```
|
|
851
|
-
|
|
852
|
-
function calculateNepaliAge(birthDate: string): {
|
|
853
|
-
years: number;
|
|
854
|
-
months: number;
|
|
855
|
-
days: number;
|
|
856
|
-
totalDays: number;
|
|
857
|
-
} {
|
|
858
|
-
const birth = new NepaliDate(birthDate);
|
|
859
|
-
const today = new NepaliDate();
|
|
860
|
-
|
|
861
|
-
let years = today.getYear() - birth.getYear();
|
|
862
|
-
let months = today.getMonth() - birth.getMonth();
|
|
863
|
-
let days = today.getDate() - birth.getDate();
|
|
864
|
-
|
|
865
|
-
// Adjust for negative days
|
|
866
|
-
if (days < 0) {
|
|
867
|
-
months--;
|
|
868
|
-
const prevMonth = today.addMonths(-1);
|
|
869
|
-
days += prevMonth.daysInMonth();
|
|
870
|
-
}
|
|
871
|
-
|
|
872
|
-
// Adjust for negative months
|
|
873
|
-
if (months < 0) {
|
|
874
|
-
years--;
|
|
875
|
-
months += 12;
|
|
876
|
-
}
|
|
877
|
-
|
|
878
|
-
const totalDays = birth.diff(today, 'day') * -1;
|
|
879
|
-
|
|
880
|
-
return { years, months, days, totalDays };
|
|
881
|
-
}
|
|
882
|
-
|
|
883
|
-
const age = calculateNepaliAge('2060-05-15');
|
|
884
|
-
console.log(`Age: ${age.years} years, ${age.months} months, ${age.days} days`);
|
|
885
|
-
```
|
|
317
|
+
### Date Range Operations
|
|
318
|
+
```ts
|
|
319
|
+
const date = new NepaliDate(2081, 5, 15);
|
|
886
320
|
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
import React, { useState } from 'react';
|
|
892
|
-
import { NepaliDate, MONTH_EN } from 'nepali-date-library';
|
|
893
|
-
|
|
894
|
-
const NepaliDatePicker: React.FC = () => {
|
|
895
|
-
const [selectedDate, setSelectedDate] = useState(new NepaliDate());
|
|
896
|
-
|
|
897
|
-
const handleDateChange = (year: number, month: number, day: number) => {
|
|
898
|
-
if (NepaliDate.isValid(year, month, day)) {
|
|
899
|
-
setSelectedDate(new NepaliDate(year, month, day));
|
|
900
|
-
}
|
|
901
|
-
};
|
|
902
|
-
|
|
903
|
-
return (
|
|
904
|
-
<div>
|
|
905
|
-
<h3>Selected Date: {selectedDate.format('MMMM DD, YYYY')}</h3>
|
|
906
|
-
<p>In Nepali: {selectedDate.format('mmmm dd, yyyy')}</p>
|
|
907
|
-
<p>Day: {selectedDate.format('DDDD')}</p>
|
|
908
|
-
<p>Gregorian: {selectedDate.getEnglishDate().toLocaleDateString()}</p>
|
|
909
|
-
|
|
910
|
-
{/* Add your date picker UI here */}
|
|
911
|
-
</div>
|
|
912
|
-
);
|
|
913
|
-
};
|
|
321
|
+
const startOfWeek = date.startOfWeek(); // Sunday of current week
|
|
322
|
+
const endOfWeek = date.endOfWeek(); // Saturday of current week
|
|
323
|
+
const startOfMonth = date.startOfMonth(); // 1st day of current month
|
|
324
|
+
const endOfYear = date.endOfYear(); // Last day of current year
|
|
914
325
|
```
|
|
915
326
|
|
|
916
|
-
###
|
|
917
|
-
```
|
|
918
|
-
|
|
919
|
-
|
|
327
|
+
### Validation
|
|
328
|
+
```ts
|
|
329
|
+
// Static validation
|
|
330
|
+
console.log(NepaliDate.isValid(2081, 4, 32)); // false (day out of range)
|
|
920
331
|
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
try {
|
|
925
|
-
const { fromCalendar, date } = req.params;
|
|
926
|
-
|
|
927
|
-
if (fromCalendar === 'ad') {
|
|
928
|
-
const bsDate = ADtoBS(date);
|
|
929
|
-
const nepaliDate = new NepaliDate(bsDate);
|
|
930
|
-
|
|
931
|
-
res.json({
|
|
932
|
-
original: date,
|
|
933
|
-
converted: bsDate,
|
|
934
|
-
formatted: {
|
|
935
|
-
english: nepaliDate.format('MMMM DD, YYYY'),
|
|
936
|
-
nepali: nepaliDate.format('mmmm dd, yyyy')
|
|
937
|
-
},
|
|
938
|
-
dayOfWeek: nepaliDate.format('DDDD'),
|
|
939
|
-
quarter: nepaliDate.getCurrentQuarter(),
|
|
940
|
-
fiscalYear: NepaliDate.getCurrentFiscalYear(),
|
|
941
|
-
isLeapYear: nepaliDate.isLeapYear()
|
|
942
|
-
});
|
|
943
|
-
} else if (fromCalendar === 'bs') {
|
|
944
|
-
const adDate = BStoAD(date);
|
|
945
|
-
const jsDate = new Date(adDate);
|
|
946
|
-
|
|
947
|
-
res.json({
|
|
948
|
-
original: date,
|
|
949
|
-
converted: adDate,
|
|
950
|
-
formatted: jsDate.toLocaleDateString('en-US', {
|
|
951
|
-
year: 'numeric',
|
|
952
|
-
month: 'long',
|
|
953
|
-
day: 'numeric'
|
|
954
|
-
})
|
|
955
|
-
});
|
|
956
|
-
} else {
|
|
957
|
-
res.status(400).json({ error: 'Invalid calendar type. Use "ad" or "bs"' });
|
|
958
|
-
}
|
|
959
|
-
} catch (error) {
|
|
960
|
-
res.status(400).json({ error: error.message });
|
|
961
|
-
}
|
|
962
|
-
});
|
|
963
|
-
|
|
964
|
-
// Fiscal year endpoints
|
|
965
|
-
app.get('/api/fiscal-year/:year/quarters', (req, res) => {
|
|
966
|
-
try {
|
|
967
|
-
const year = parseInt(req.params.year);
|
|
968
|
-
const quarters = NepaliDate.getFiscalYearQuarters(year);
|
|
969
|
-
|
|
970
|
-
const response = Object.entries(quarters).map(([quarter, dates]) => ({
|
|
971
|
-
quarter,
|
|
972
|
-
startDate: dates.start.format('YYYY-MM-DD'),
|
|
973
|
-
endDate: dates.end.format('YYYY-MM-DD'),
|
|
974
|
-
startFormatted: dates.start.format('MMMM DD, YYYY'),
|
|
975
|
-
endFormatted: dates.end.format('MMMM DD, YYYY')
|
|
976
|
-
}));
|
|
977
|
-
|
|
978
|
-
res.json(response);
|
|
979
|
-
} catch (error) {
|
|
980
|
-
res.status(400).json({ error: error.message });
|
|
981
|
-
}
|
|
982
|
-
});
|
|
332
|
+
// Instance validation
|
|
333
|
+
const date = new NepaliDate(2081, 5, 10);
|
|
334
|
+
console.log(date.isValid()); // true
|
|
983
335
|
```
|
|
984
336
|
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
337
|
+
### Name Utilities
|
|
338
|
+
```ts
|
|
339
|
+
// Month names
|
|
340
|
+
console.log(NepaliDate.getMonthName(0, false, false)); // "Baisakh"
|
|
341
|
+
console.log(NepaliDate.getMonthName(0, true, true)); // "बै"
|
|
990
342
|
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
expect(ADtoBS('2024-12-15')).toBe('2081-08-30');
|
|
995
|
-
});
|
|
996
|
-
|
|
997
|
-
test('should convert BS to AD correctly', () => {
|
|
998
|
-
expect(BStoAD('2081-10-14')).toBe('2025-02-26');
|
|
999
|
-
expect(BStoAD('2081-05-15')).toBe('2024-08-30');
|
|
1000
|
-
});
|
|
1001
|
-
|
|
1002
|
-
test('should handle date arithmetic correctly', () => {
|
|
1003
|
-
const date = new NepaliDate(2081, 9, 15);
|
|
1004
|
-
const futureDate = date.addDays(10);
|
|
1005
|
-
|
|
1006
|
-
expect(futureDate.getDate()).toBe(25);
|
|
1007
|
-
expect(futureDate.getMonth()).toBe(9);
|
|
1008
|
-
expect(futureDate.getYear()).toBe(2081);
|
|
1009
|
-
});
|
|
1010
|
-
|
|
1011
|
-
test('should validate dates correctly', () => {
|
|
1012
|
-
expect(NepaliDate.isValid(2081, 4, 31)).toBe(true);
|
|
1013
|
-
expect(NepaliDate.isValid(2081, 4, 32)).toBe(false);
|
|
1014
|
-
expect(NepaliDate.isValid(2081, 12, 1)).toBe(false); // Month out of range
|
|
1015
|
-
});
|
|
1016
|
-
|
|
1017
|
-
test('should handle fiscal year calculations', () => {
|
|
1018
|
-
const currentFY = NepaliDate.getCurrentFiscalYear();
|
|
1019
|
-
const quarters = NepaliDate.getFiscalYearQuarters(currentFY);
|
|
1020
|
-
|
|
1021
|
-
expect(Object.keys(quarters)).toEqual(['Q1', 'Q2', 'Q3', 'Q4']);
|
|
1022
|
-
expect(quarters.Q1.start.getMonth()).toBe(3); // Shrawan (index 3)
|
|
1023
|
-
});
|
|
1024
|
-
});
|
|
343
|
+
// Day names
|
|
344
|
+
console.log(NepaliDate.getDayName(0, false, true)); // "आइतबार"
|
|
345
|
+
console.log(NepaliDate.getDayName(1, true, false)); // "Mon"
|
|
1025
346
|
```
|
|
1026
347
|
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
### Why are months 0-indexed?
|
|
1030
|
-
To maintain consistency with JavaScript's Date object and provide seamless integration with existing JavaScript/TypeScript codebases.
|
|
348
|
+
---
|
|
1031
349
|
|
|
1032
|
-
|
|
1033
|
-
|
|
350
|
+
## Error Handling
|
|
351
|
+
- Throws an error if an invalid date format is used in constructor or parse method
|
|
352
|
+
- Throws an error if the Nepali date is out of the supported range
|
|
353
|
+
- Validates input parameters and throws appropriate errors for invalid values
|
|
1034
354
|
|
|
1035
|
-
|
|
1036
|
-
|
|
355
|
+
## Supported Date Range
|
|
356
|
+
The library supports Nepali dates within a specific range. Use `NepaliDate.minimum()` and `NepaliDate.maximum()` to get the supported date boundaries.
|
|
1037
357
|
|
|
1038
|
-
|
|
1039
|
-
|
|
358
|
+
## Acknowledgements
|
|
359
|
+
This project was inspired by [nepali-date](https://github.com/sharingapples/nepali-date).
|
|
1040
360
|
|
|
1041
|
-
|
|
1042
|
-
|
|
361
|
+
## License
|
|
362
|
+
MIT License
|