Skip to content

Commit 3368c0d

Browse files
committed
fix: properly floor the partial results
1 parent 83b4dd8 commit 3368c0d

File tree

2 files changed

+48
-36
lines changed

2 files changed

+48
-36
lines changed

Conversions/DateDayDifference.js

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,27 @@ const isLeap = (year) => {
1414
else return false
1515
}
1616
const DateToDay = (dd, mm, yyyy) => {
17-
return Math.floor(
17+
return (
1818
365 * (yyyy - 1) +
19-
(yyyy - 1) / 4 -
20-
(yyyy - 1) / 100 +
21-
(yyyy - 1) / 400 +
22-
dd +
23-
(367 * mm - 362) / 12 +
24-
(mm <= 2 ? 0 : isLeap(yyyy) ? -1 : -2)
19+
Math.floor((yyyy - 1) / 4) -
20+
Math.floor((yyyy - 1) / 100) +
21+
Math.floor((yyyy - 1) / 400) +
22+
dd +
23+
Math.floor((367 * mm - 362) / 12) +
24+
(mm <= 2 ? 0 : isLeap(yyyy) ? -1 : -2)
2525
)
2626
}
2727

28+
const CheckDayAndMonth = (inDay, inMonth) => {
29+
if (inDay <= 0 || inDay > 31 || inMonth <= 0 || inMonth > 12) {
30+
throw new TypeError('Date is not valid.')
31+
}
32+
}
33+
2834
const DateDayDifference = (date1, date2) => {
2935
// firstly, check that both input are string or not.
3036
if (typeof date1 !== 'string' || typeof date2 !== 'string') {
31-
return new TypeError('Argument is not a string.')
37+
throw new TypeError('Argument is not a string.')
3238
}
3339
// extract the first date
3440
const [firstDateDay, firstDateMonth, firstDateYear] = date1
@@ -39,18 +45,9 @@ const DateDayDifference = (date1, date2) => {
3945
.split('/')
4046
.map((ele) => Number(ele))
4147
// check the both data are valid or not.
42-
if (
43-
firstDateDay < 0 ||
44-
firstDateDay > 31 ||
45-
firstDateMonth > 12 ||
46-
firstDateMonth < 0 ||
47-
secondDateDay < 0 ||
48-
secondDateDay > 31 ||
49-
secondDateMonth > 12 ||
50-
secondDateMonth < 0
51-
) {
52-
return new TypeError('Date is not valid.')
53-
}
48+
CheckDayAndMonth(firstDateDay, firstDateMonth)
49+
CheckDayAndMonth(secondDateDay, secondDateMonth)
50+
5451
return Math.abs(
5552
DateToDay(secondDateDay, secondDateMonth, secondDateYear) -
5653
DateToDay(firstDateDay, firstDateMonth, firstDateYear)
Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
11
import { DateDayDifference } from '../DateDayDifference'
22

3-
test('The difference between 17/08/2002 & 10/10/2020 is 6630', () => {
4-
const res = DateDayDifference('17/08/2002', '10/10/2020')
5-
expect(res).toBe(6630)
6-
})
7-
8-
test('The difference between 18/02/2001 & 16/03/2022 is 7696', () => {
9-
const res = DateDayDifference('18/02/2001', '16/03/2022')
10-
expect(res).toBe(7696)
11-
})
3+
describe('DateDayDifference', () => {
4+
it.each([
5+
['17/08/2002', '10/10/2020', 6629],
6+
['18/02/2001', '16/03/2022', 7696],
7+
['11/11/2011', '12/12/2012', 397],
8+
['01/01/2001', '16/03/2011', 3726],
9+
['04/03/2024', '04/03/2024', 0],
10+
['03/03/2024', '04/03/2024', 1],
11+
['02/03/2024', '04/03/2024', 2],
12+
['01/03/2024', '04/03/2024', 3],
13+
['29/02/2024', '04/03/2024', 4],
14+
['04/03/2024', '04/03/2025', 365],
15+
['04/03/2023', '04/03/2024', 366]
16+
])(
17+
'The difference between %s and %s is %i',
18+
(firstDate, secondDate, expected) => {
19+
expect(DateDayDifference(firstDate, secondDate)).toBe(expected)
20+
expect(DateDayDifference(secondDate, firstDate)).toBe(expected)
21+
}
22+
)
1223

13-
test('The difference between 11/11/2011 & 12/12/2012 is 398', () => {
14-
const res = DateDayDifference('11/11/2011', '12/12/2012')
15-
expect(res).toBe(398)
16-
})
24+
it('should throw when any input is not a string', () => {
25+
expect(() => DateDayDifference(10102024, '11/10/2024')).toThrowError()
26+
expect(() => DateDayDifference('11/10/2024', 10102024)).toThrowError()
27+
})
1728

18-
test('The difference between 01/01/2001 & 16/03/2011 is 3727', () => {
19-
const res = DateDayDifference('01/01/2001', '16/03/2011')
20-
expect(res).toBe(3727)
29+
it.each(['32/01/2000', '00/01/2000', '15/00/2000', '15/13/2000'])(
30+
'should throw when input is not a correct date %s',
31+
(wrongDate) => {
32+
expect(() => DateDayDifference(wrongDate, '04/03/2024')).toThrowError()
33+
expect(() => DateDayDifference('04/03/2024', wrongDate)).toThrowError()
34+
}
35+
)
2136
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy