Skip to content

Commit 6ae01df

Browse files
committed
added test cases and getDaysBetweenTwoAdDates function to ignore partial days
1 parent 019ce58 commit 6ae01df

File tree

5 files changed

+16
-6
lines changed

5 files changed

+16
-6
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: 1 addition & 4 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()
@@ -184,9 +184,6 @@ export class BikramSambat {
184184

185185
const gregorianDate = new Date(date)
186186

187-
// Ensure the date is normalized to midnight UTC
188-
gregorianDate.setUTCHours(0, 0, 0, 0)
189-
190187
if (isNaN(gregorianDate.getTime())) {
191188
return new BikramSambat(InvalidDate)
192189
}

src/index.ts

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

3-
export default BikramSambat
3+
4+
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)