Skip to content

Commit d347560

Browse files
authored
fixed inconsistent output issue (#58)
2 parents 6bb0203 + 669c68d commit d347560

File tree

5 files changed

+19
-4
lines changed

5 files changed

+19
-4
lines changed

src/BikramSambat.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ describe('BikramSambat Class', () => {
9393
expect(bikramSambat.toAD().toISOString().slice(0, 10)).toBe(adDate)
9494
})
9595
})
96+
it('should properly convert dates to BikramSambat using both instance and static methods', () => {
97+
const bsDateFromInstance = new BikramSambat().toString()
98+
const bsDateFromStaticMethod = BikramSambat.fromAD(new Date()).toString();
99+
expect(bsDateFromInstance).toEqual(bsDateFromStaticMethod)
100+
})
96101
it('should properly convert to BikramSambat Date', () => {
97102
sampleData.forEach(([bsDate, adDate]) => {
98103
expect(BikramSambat.fromAD(adDate).toString()).toBe(bsDate)

src/BikramSambat.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export class BikramSambat {
4949
this.month = dateStr.getMonth()
5050
this.day = dateStr.getDay()
5151
} else {
52-
const currentDate = new Date().toISOString().slice(0, 10)
52+
const currentDate = new Date()
5353
const currentBsDate = BikramSambat.fromAD(currentDate)
5454
this.year = currentBsDate.getYear()
5555
this.month = currentBsDate.getMonth()
@@ -181,10 +181,13 @@ export class BikramSambat {
181181
if (!date) {
182182
return new BikramSambat()
183183
}
184+
184185
const gregorianDate = new Date(date)
185-
if (gregorianDate.toString() === InvalidDate) {
186+
187+
if (isNaN(gregorianDate.getTime()) || gregorianDate.toString() === InvalidDate) {
186188
return new BikramSambat(InvalidDate)
187189
}
190+
188191
const { newYearDate, bsYear } = getNewYearDateInfo(gregorianDate)
189192
const daysFromNewYear = getDaysBetweenTwoAdDates(
190193
gregorianDate,

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import { BikramSambat } from './BikramSambat'
22

3-
export default BikramSambat
3+
export default BikramSambat

src/utils/getDaysBetweenTwoAdDates.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,11 @@ describe('getDaysBetweenTwoAdDates', () => {
1818
const endDate = new Date('2023-08-15')
1919
expect(getDaysBetweenTwoAdDates(startDate, endDate)).toBe(0)
2020
})
21+
22+
it('calculates the correct number of days ignoring partial days', () => {
23+
const startDate = new Date();
24+
const endDate = new Date();
25+
endDate.setMinutes(startDate.getMinutes() + 5); // Add 5 minutes
26+
expect(getDaysBetweenTwoAdDates(startDate, endDate)).toBe(0)
27+
})
2128
})

src/utils/getDaysBetweenTwoAdDates.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ const MILLISECONDS_IN_A_DAY = 1000 * 60 * 60 * 24
22

33
export const getDaysBetweenTwoAdDates = (startDate: Date, endDate: Date) => {
44
const timeDiff = Math.abs(endDate.getTime() - startDate.getTime())
5-
const diffDays = Math.ceil(timeDiff / MILLISECONDS_IN_A_DAY)
5+
const diffDays = Math.floor(timeDiff / MILLISECONDS_IN_A_DAY)
66
return diffDays
77
}

0 commit comments

Comments
 (0)