forked from opensource-nepal/node-nepali-datetime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNepaliDate.ts
607 lines (557 loc) · 17.2 KB
/
NepaliDate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
import dateConverter from './dateConverter'
import { format, formatNepali, nepaliDateToString } from './format'
import { parse, parseFormat } from './parse'
import { getDate, getNepalDateAndTime } from './utils'
import { validateTime } from './validators'
/**
* Represents a Nepali calendar date.
*/
class NepaliDate {
private timestamp: Date
private year: number
private yearEn: number
private month: number
private monthEn: number
private day: number
private dayEn: number
private hour: number
private minute: number
private weekDay: number
static minimum: () => Date
static maximum: () => Date
/**
* Creates a NepaliDate instance for the current date and time.
*
* @example
* const now = new NepaliDate()
*/
constructor()
/**
* Creates a NepaliDate instance from a provided Javascript Date object.
*
* @param {Date} date - The Javascript Date object.
*
* @example
* const jsDate = new Date("2020-01-01")
* const nepaliDate = new NepaliDate(jsDate)
*/
constructor(date: Date)
/**
* Creates a new NepaliDate instance from another NepaliDate object.
*
* @param {NepaliDate} date - The NepaliDate object.
* @example
* const nepaliDateOld = new NepaliDate('2080-01-01')
* const nepaliDate = new NepaliDate(nepaliDateOld)
*/
constructor(date: NepaliDate)
/**
* Creates a NepaliDate instance by parsing a provided date-time string.
*
* @param {string} value - The date-time string.
* @example
* const nepaliDate = new NepaliDate('2080-01-01')
*/
constructor(value: string)
/**
* Creates a NepaliDate instance from a provided Unix timestamp.
*
* @param {number} value - The Unix timestamp.
* @example
* const timestamp = 1695569762 // Unix timestamp in milliseconds
* const nepaliDate = new NepaliDate(timestamp)
*/
constructor(value: number)
/**
* Creates a NepaliDate instance by parsing a provided date-time string
* with the given format.
*
* @param dateString - The date-time string to parse.
* @param format - The format of the provided date-time string.
* @example
* const dateTimeString = '2080/08/12 14-05-23.789'
* const format = 'YYYY/MM/DD HH-mm-ss.SSS'
* const nepaliDate = new NepaliDate(dateTimeString, format)
*/
constructor(dateString: string, format: string)
/**
* Creates a NepaliDate instance from a provided Nepali calendar date components.
*
* @constructor
* @param {number} year - The year (e.g., 2080).
* @param {number} month - The month (0-11, where 0 is Baisakh and 11 is Chaitra).
* @param {number} [day=1] - The day of the month (1-31).
* @param {number} [hour=0] - The hour of the day (0-23).
* @param {number} [minute=0] - The minute (0-59).
* @param {number} [second=0] - The second (0-59).
* @param {number} [ms=0] - The milliseconds (0-999).
*
* @example
* const year = 2080
* const month = 1 // Jestha
* const day = 12
* const hour = 12
* const minute = 30
* const second = 45
* const ms = 500
* const nepaliDate = new NepaliDate(year, month, day, hour, minute, second, ms)
*/
constructor(
year: number,
month: number,
day?: number,
hour?: number,
minute?: number,
second?: number,
ms?: number
)
constructor(...args: any[]) {
if (args.length === 0) {
this.initFromCurrentDate()
} else if (args.length === 1 && args[0] instanceof Date) {
this.initFromEnglishDate(args[0])
} else if (args.length === 1 && args[0] instanceof NepaliDate) {
this.initFromNepaliDate(args[0])
} else if (args.length === 1 && typeof args[0] === 'string') {
this.parseFromString(args[0])
} else if (args.length === 1 && typeof args[0] === 'number') {
this.initFromTimestamp(args[0])
} else if (
args.length === 2 &&
typeof args[0] === 'string' &&
typeof args[1] === 'string'
) {
this.parseFromStringWithFormat(args[0], args[1])
} else if (
args.length >= 2 &&
args.length <= 8 &&
args.every(arg => typeof arg === 'number')
) {
this.initFromComponents(args)
} else {
throw new Error('Invalid date argument')
}
}
/* Object initialization */
private initFromCurrentDate() {
this._setDateObject(new Date())
}
private initFromEnglishDate(date: Date) {
this._setDateObject(date)
}
private initFromNepaliDate(date: NepaliDate) {
this.set(
date.year,
date.month,
date.day,
date.hour,
date.minute,
date.getSeconds(),
date.getMilliseconds()
)
}
private parseFromString(value: string) {
const parsedResult = parse(value)
this.set(
parsedResult[0], // Year
parsedResult[1], // Month
parsedResult[2], // Date
parsedResult[3], // Hour
parsedResult[4], // Minute
parsedResult[5], // Second
parsedResult[6] // Millisecond
)
}
private initFromTimestamp(value: number) {
this._setDateObject(new Date(value))
}
private parseFromStringWithFormat(dateString: string, format: string) {
const parsedResult = parseFormat(dateString, format)
this.set(
parsedResult[0], // Year
parsedResult[1], // Month
parsedResult[2], // Date
parsedResult[3], // Hour
parsedResult[4], // Minute
parsedResult[5], // Second
parsedResult[6] // Millisecond
)
}
private initFromComponents(args: number[]) {
this.set(
args[0], // year
args[1], // month
args[2] ?? 1, // day
args[3] ?? 0, // hour
args[4] ?? 0, // minute
args[5] ?? 0, // second
args[6] ?? 0 // ms
)
}
/* Object methods */
/**
* Sets the English date and optionally computes the corresponding Nepali date.
* Handles all the operations and variables while setting the English date.
*
* @param date The English date to set.
* @param computeNepaliDate Flag indicating whether to compute the Nepali date. Default is `true`.
* @returns void
*/
private _setDateObject(date: Date, computeNepaliDate: boolean = true) {
this.timestamp = date
// getting Nepal's hour, minute, and weekDay
const { year, month0, day, hour, minute, weekDay } = getNepalDateAndTime(date)
this.yearEn = year
this.monthEn = month0
this.dayEn = day
this.hour = hour
this.minute = minute
this.weekDay = weekDay
if (computeNepaliDate) {
const [yearNp, month0Np, dayNp] = dateConverter.englishToNepali(
year,
month0,
day
)
this.year = yearNp
this.month = month0Np
this.day = dayNp
}
}
/**
* Retrieves the Date object equivalent to the NepaliDate.
*
* @returns {Date} The equivalent JavaScript Date object.
*/
getDateObject() {
return this.timestamp
}
/**
* Retrieves the year of the Nepali date in the Nepali calendar.
*
* @returns {number} The full numeric value representing the year. Eg. 2080
*/
getYear(): number {
return this.year
}
/**
* Retrieves the year of the Nepali date in the English calendar.
*
* @returns {number} The full numeric value representing the year. Eg. 2009
*/
getEnglishYear(): number {
return this.yearEn
}
/**
* Retrieves the month of the Nepali date in the Nepali calendar.
*
* @returns {number} The numeric value representing the month. 0 for Baisakh and 11 for Chaitra.
*/
getMonth(): number {
return this.month
}
/**
* Retrieves the month of the Nepali date in the English calendar.
*
* @returns {number} The numeric value representing the month. 0 for January and 11 for December.
*/
getEnglishMonth(): number {
return this.monthEn
}
/**
* Retrieves the day of the month represented of Nepali date in Nepali calendar.
*
* @returns {number} The numeric value representing the day of the month.
*/
getDate(): number {
return this.day
}
/**
* Retrieves the day of the month represented of Nepali date in English calendar.
*
* @returns {number} The numeric value representing the day of the month.
*/
getEnglishDate(): number {
return this.dayEn
}
/**
* Retrieves the day of the week represented by a numeric value.
*
* @returns The numeric value representing the day of the week.
* 0: Sunday
* 1: Monday
* 2: Tuesday
* 3: Wednesday
* 4: Thursday
* 5: Friday
* 6: Saturday
*/
getDay(): number {
return this.weekDay
}
/**
* Retrieves the hour value of the Nepali date.
*
* @returns {number} The numeric value representing the hour.
*/
getHours(): number {
return this.hour
}
/**
* Retrieves the minute value of the Nepali date.
*
* @returns {number} The numeric value representing the minute.
*/
getMinutes(): number {
return this.minute
}
/**
* Retrieves the second value of the Nepali date.
*
* @returns {number} The numeric value representing the second.
*/
getSeconds(): number {
return this.timestamp.getSeconds()
}
/**
* Retrieves the millisecond value of the Nepali date.
*
* @returns {number} The numeric value representing the millisecond.
*/
getMilliseconds(): number {
return this.timestamp.getMilliseconds()
}
/**
* Retrieves the unix timestamp (in milliseconds) of the Nepali date.
*
* @returns {number} The numeric value representing the time in milliseconds.
*/
getTime(): number {
return this.timestamp.getTime()
}
/**
* Sets the day on the current date and time
*
* @param {number} year - The numeric value representing the year.
* @throws {ValidationError} if year is out of range
*/
setYear(year: number) {
this.set(
year,
this.month,
this.day,
this.hour,
this.minute,
this.timestamp.getSeconds(),
this.timestamp.getMilliseconds()
)
}
/**
* Sets the day on the current date and time
*
* @param {number} month - The numeric value representing the month.
* @throws {ValidationError} if month is out of range
*/
setMonth(month: number) {
this.set(
this.year,
month,
this.day,
this.hour,
this.minute,
this.timestamp.getSeconds(),
this.timestamp.getMilliseconds()
)
}
/**
* Sets the day on the current date and time
*
* @param {number} day - The numeric value representing the day.
* @throws {ValidationError} if day is out of range
*/
setDate(day: number) {
this.set(
this.year,
this.month,
day,
this.hour,
this.minute,
this.timestamp.getSeconds(),
this.timestamp.getMilliseconds()
)
}
/**
* Sets hour on the current date and time
*
* @param hour Hour to set
* @throws {ValidationError} if hour is out of range
*/
setHours(hour: number) {
this.set(
this.year,
this.month,
this.day,
hour,
this.minute,
this.timestamp.getSeconds(),
this.timestamp.getMilliseconds()
)
}
/**
* Sets minute on the current date and time
*
* @param minute Minute to set
* @throws {ValidationError} if minute is out of range
*/
setMinutes(minute: number) {
this.set(
this.year,
this.month,
this.day,
this.hour,
minute,
this.timestamp.getSeconds(),
this.timestamp.getMilliseconds()
)
}
/**
* Sets second on the current date and time
*
* @param second Second to set
* @throws {ValidationError} if second is out of range
*/
setSeconds(second: number) {
this.set(
this.year,
this.month,
this.day,
this.hour,
this.minute,
second,
this.timestamp.getMilliseconds()
)
}
/**
* Sets milliseconds on the current date and time
*
* @param ms Milliseconds to set
* @throws {ValidationError} if milliseconds is out of range
*/
setMilliseconds(ms: number) {
this.set(
this.year,
this.month,
this.day,
this.hour,
this.minute,
this.timestamp.getSeconds(),
ms
)
}
/**
* Sets time on the object.
*
* @param time Time to set (timestamp)
*/
setTime(time: number) {
this._setDateObject(new Date(time))
}
/**
* Sets the Nepali date and time values.
*
* @param {number} year - The numeric value representing the year.
* @param {number} month - The numeric value representing the month.
* @param {number} date - The numeric value representing the day.
* @param {number} [hour=0] - The numeric value representing the hour.
* @param {number} [minute=0] - The numeric value representing the minute.
* @param {number} [second=0] - The numeric value representing the second.
* @param {number} [ms=0] - The numeric value representing the millisecond.
*/
set(
year: number,
month: number,
date: number,
hour: number,
minute: number,
second: number,
ms: number
) {
validateTime(hour, minute, second, ms)
const [yearEn, month0EN, dayEn] = dateConverter.nepaliToEnglish(
year,
month,
date
)
this.year = year
this.month = month
this.day = date
this._setDateObject(
getDate(yearEn, month0EN, dayEn, hour, minute, second, ms),
false
)
}
/**
* Sets the Date object on the current NepaliDate object.
*
* @param date The Date object to set.
* @returns void
*/
setDateObject(date: Date) {
this._setDateObject(date, true) // forcing to compute nepali date
}
/**
* Returns a string representation (in English) of the NepaliDate object in the specified format.
*
* @param {string} formatStr - The format string specifying the desired format.
* @returns {string} The formatted Nepali date string.
*/
format(formatStr: string): string {
return format(this, formatStr)
}
/**
* Returns a string representation in the Nepali (Devanagari) of the NepaliDate object in the specified format.
* @param formatStr The format string for the desired output.
* @returns {string} A string representation of the NepaliDate object in the specified format.
*/
formatNepali(formatStr: string): string {
return formatNepali(this, formatStr)
}
/**
* Returns a string representation of the NepaliDate object.
*
* @returns {string} The string representation of the Nepali date.
*/
toString(): string {
return nepaliDateToString(this)
}
/* Static methods */
/**
* Creates a new instance of NepaliDate from an English calendar parameters.
*
* @param year - The year in English calendar format.
* @param month0 - The month (0-11) in English calendar format.
* @param date - The day of the month in English calendar format.
* @param hour - The hour (0-23) in English calendar format. Default is 0.
* @param minute - The minute (0-59) in English calendar format. Default is 0.
* @param second - The second (0-59) in English calendar format. Default is 0.
* @param ms - The millisecond (0-999) in English calendar format. Default is 0.
* @returns A new instance of NepaliDate corresponding to the provided English date.
*/
static fromEnglishDate(
year: number,
month0: number,
date: number,
hour: number = 0,
minute: number = 0,
second: number = 0,
ms: number = 0
): NepaliDate {
const englishDate = getDate(year, month0, date, hour, minute, second, ms)
return new NepaliDate(englishDate)
}
}
NepaliDate.minimum = () =>
NepaliDate.fromEnglishDate(dateConverter.enMinYear(), 0, 1).getDateObject()
NepaliDate.maximum = () =>
NepaliDate.fromEnglishDate(dateConverter.enMaxYear(), 11, 31).getDateObject()
export default NepaliDate