Skip to content

Commit c85edfa

Browse files
authored
feat: add static method getDaysOfMonth on NepaliDate (#96)
1 parent 577df7f commit c85edfa

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ const date2 = NepaliDate.fromEnglishDate(2023, 6, 8, 10, 15)
215215
console.log(date2.toString()) // 2080-03-23 10:15:00
216216
```
217217

218+
#### Others
219+
220+
- `NepaliDate.getDaysOfMonth(year, month)`: Returns the number of days in a specific month of a given year.
221+
218222
### dateConverter
219223

220224
The `dateConverter` module provides core functions for converting dates between the Nepali and English calendars.

src/NepaliDate.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import dateConverter from './dateConverter'
2+
import { NP_MONTHS_DATA } from './dateConverter/constants'
23
import {
34
format,
45
formatEnglishDate,
@@ -642,6 +643,26 @@ class NepaliDate {
642643
)
643644
return NepaliDate.fromEnglishDate(year, month0, day, hour, minute, second, ms)
644645
}
646+
647+
/**
648+
* Returns the number of days in a specific month of a given year.
649+
*
650+
* @param year - The year to fetch the month from.
651+
* @param month - The month to get the number of days for.
652+
* @returns The number of days in the specified month.
653+
* @throws {Error} If the year or month is out of range.
654+
*/
655+
static getDaysOfMonth(year: number, month: number): number {
656+
if (year < dateConverter.npMinYear() || year > dateConverter.npMaxYear()) {
657+
throw new Error('Year out of range')
658+
}
659+
660+
if (month < 0 || month > 11) {
661+
throw new Error('Month out of range')
662+
}
663+
664+
return NP_MONTHS_DATA[year - dateConverter.npMinYear()][0][month]
665+
}
645666
}
646667

647668
NepaliDate.minimum = () =>

tests/NepaliDate.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import dateConverter from '../src/dateConverter'
12
import NepaliDate from '../src/NepaliDate'
23
import { ValidationError } from '../src/validators'
34

@@ -741,3 +742,48 @@ describe('NepaliDate with Time feature: set methods', () => {
741742
})
742743
})
743744
})
745+
746+
describe('NepaliDate.getDaysOfMonth', () => {
747+
it('should return the correct number of days for a valid year and month', () => {
748+
const days = NepaliDate.getDaysOfMonth(2081, 8)
749+
expect(days).toBe(29)
750+
})
751+
752+
it('should return the correct number of days for a year 2000 and month 0', () => {
753+
const days = NepaliDate.getDaysOfMonth(2000, 0)
754+
expect(days).toBe(30)
755+
})
756+
757+
it('should return the correct number of days for a year 2099 and month 11', () => {
758+
const days = NepaliDate.getDaysOfMonth(2099, 11)
759+
expect(days).toBe(30)
760+
})
761+
762+
it('should throw an error if the year is below the valid range', () => {
763+
const invalidYear = dateConverter.npMinYear() - 1
764+
expect(() => NepaliDate.getDaysOfMonth(invalidYear, 0)).toThrow(
765+
'Year out of range'
766+
)
767+
})
768+
769+
it('should throw an error if the year is above the valid range', () => {
770+
const invalidYear = dateConverter.npMaxYear() + 1
771+
expect(() => NepaliDate.getDaysOfMonth(invalidYear, 0)).toThrow(
772+
'Year out of range'
773+
)
774+
})
775+
776+
it('should throw an error if the month is below 0', () => {
777+
const validYear = dateConverter.npMinYear()
778+
expect(() => NepaliDate.getDaysOfMonth(validYear, -1)).toThrow(
779+
'Month out of range'
780+
)
781+
})
782+
783+
it('should throw an error if the month is greater than 11', () => {
784+
const validYear = dateConverter.npMinYear()
785+
expect(() => NepaliDate.getDaysOfMonth(validYear, 12)).toThrow(
786+
'Month out of range'
787+
)
788+
})
789+
})

0 commit comments

Comments
 (0)