Skip to content

Commit b4d05b4

Browse files
committed
refactor: use isLeapYear
1 parent d8cfdcd commit b4d05b4

File tree

3 files changed

+25
-23
lines changed

3 files changed

+25
-23
lines changed

Conversions/DateDayDifference.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,8 @@
66
Algorithm & Explanation : https://ncalculators.com/time-date/date-difference-calculator.htm
77
*/
88

9-
// Internal method for make calculations easier
10-
const isLeap = (year) => {
11-
if (year % 400 === 0) return true
12-
else if (year % 100 === 0) return false
13-
else if (year % 4 === 0) return true
14-
else return false
15-
}
9+
import { isLeapYear } from '../Maths/LeapYear'
10+
1611
const DateToDay = (dd, mm, yyyy) => {
1712
return (
1813
365 * (yyyy - 1) +
@@ -21,7 +16,7 @@ const DateToDay = (dd, mm, yyyy) => {
2116
Math.floor((yyyy - 1) / 400) +
2217
dd +
2318
Math.floor((367 * mm - 362) / 12) +
24-
(mm <= 2 ? 0 : isLeap(yyyy) ? -1 : -2)
19+
(mm <= 2 ? 0 : isLeapYear(yyyy) ? -1 : -2)
2520
)
2621
}
2722

Timing-Functions/GetMonthDays.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
e.g.: mahfoudh.arous.com ->false
77
*/
88

9+
import { isLeapYear } from '../Maths/LeapYear'
10+
911
const getMonthDays = (monthNumber, year) => {
1012
const the31DaysMonths = [1, 3, 5, 7, 8, 10, 12]
1113
const the30DaysMonths = [4, 6, 9, 11]
@@ -26,11 +28,8 @@ const getMonthDays = (monthNumber, year) => {
2628
return 30
2729
}
2830

29-
// Check for Leap year
30-
if (year % 4 === 0) {
31-
if (year % 100 !== 0 || (year % 100 === 0 && year % 400 === 0)) {
32-
return 29
33-
}
31+
if (isLeapYear(year)) {
32+
return 29
3433
}
3534

3635
return 28

Timing-Functions/test/GetMonthDays.test.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
import { getMonthDays } from '../GetMonthDays'
22

33
describe('Get the Days of a Month', () => {
4-
it('expects to return 28', () => {
5-
expect(getMonthDays(2, 2018)).toEqual(28)
6-
})
7-
8-
it('expects to return 30', () => {
9-
expect(getMonthDays(6, 254)).toEqual(30)
10-
})
11-
12-
it('expects to return 29', () => {
13-
expect(getMonthDays(2, 2024)).toEqual(29)
4+
it.each([
5+
[2, 2018, 28],
6+
[6, 2024, 30],
7+
[1, 2024, 31],
8+
[2, 2024, 29],
9+
[2, 2023, 28],
10+
[3, 2024, 31],
11+
[4, 2024, 30],
12+
[5, 2024, 31],
13+
[6, 2024, 30],
14+
[7, 2024, 31],
15+
[8, 2024, 31],
16+
[9, 2024, 30],
17+
[10, 2024, 31],
18+
[11, 2024, 30],
19+
[12, 2024, 31]
20+
])('Month %i in year %i has %i days', (month, year, expected) => {
21+
expect(getMonthDays(month, year)).toBe(expected)
1422
})
1523

1624
it('expects to throw a type error', () => {

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