Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9c622dd

Browse files
vil02appgurueu
andauthored
refactor: add and use parseDate (TheAlgorithms#1643)
* refactor: add and use `parseDate` * style: use proper spelling Co-authored-by: appgurueu <34514239+appgurueu@users.noreply.github.com> --------- Co-authored-by: appgurueu <34514239+appgurueu@users.noreply.github.com>
1 parent 34a663a commit 9c622dd

File tree

4 files changed

+78
-32
lines changed

4 files changed

+78
-32
lines changed

‎Conversions/DateDayDifference.js

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
import { isLeapYear } from '../Maths/LeapYear'
10+
import { parseDate } from '../Timing-Functions/ParseDate'
1011

1112
const DateToDay = (dd, mm, yyyy) => {
1213
return (
@@ -20,32 +21,13 @@ const DateToDay = (dd, mm, yyyy) => {
2021
)
2122
}
2223

23-
const CheckDayAndMonth = (inDay, inMonth) => {
24-
if (inDay <= 0 || inDay > 31 || inMonth <= 0 || inMonth > 12) {
25-
throw new TypeError('Date is not valid.')
26-
}
27-
}
28-
2924
const DateDayDifference = (date1, date2) => {
30-
// firstly, check that both input are string or not.
31-
if (typeof date1 !== 'string' || typeof date2 !== 'string') {
32-
throw new TypeError('Argument is not a string.')
33-
}
34-
// extract the first date
35-
const [firstDateDay, firstDateMonth, firstDateYear] = date1
36-
.split('/')
37-
.map((ele) => Number(ele))
38-
// extract the second date
39-
const [secondDateDay, secondDateMonth, secondDateYear] = date2
40-
.split('/')
41-
.map((ele) => Number(ele))
42-
// check the both data are valid or not.
43-
CheckDayAndMonth(firstDateDay, firstDateMonth)
44-
CheckDayAndMonth(secondDateDay, secondDateMonth)
25+
const firstDate = parseDate(date1)
26+
const secondDate = parseDate(date2)
4527

4628
return Math.abs(
47-
DateToDay(secondDateDay,secondDateMonth,secondDateYear) -
48-
DateToDay(firstDateDay,firstDateMonth,firstDateYear)
29+
DateToDay(secondDate.day,secondDate.month,secondDate.year) -
30+
DateToDay(firstDate.day,firstDate.month,firstDate.year)
4931
)
5032
}
5133

‎Conversions/DateToDay.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
Algorithm & Explanation : https://en.wikipedia.org/wiki/Zeller%27s_congruence
1313
*/
1414

15+
import { parseDate } from '../Timing-Functions/ParseDate'
16+
1517
// Array holding name of the day: Saturday - Sunday - Friday => 0 - 1 - 6
1618
const daysNameArr = [
1719
'Saturday',
@@ -25,15 +27,10 @@ const daysNameArr = [
2527

2628
const DateToDay = (date) => {
2729
// firstly, check that input is a string or not.
28-
if (typeof date !== 'string') {
29-
throw new TypeError('Argument is not a string.')
30-
}
31-
// extract the date
32-
let [day, month, year] = date.split('/').map((x) => Number(x))
33-
// check the data are valid or not.
34-
if (day < 1 || day > 31 || month > 12 || month < 1) {
35-
throw new TypeError('Date is not valid.')
36-
}
30+
const dateStruct = parseDate(date)
31+
let year = dateStruct.year
32+
let month = dateStruct.month
33+
let day = dateStruct.day
3734

3835
// In case of Jan and Feb:
3936
// Year: we consider it as previous year

‎Timing-Functions/ParseDate.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { getMonthDays } from './GetMonthDays'
2+
3+
function checkDate(date) {
4+
if (date.day < 1 || date.day > getMonthDays(date.month, date.year)) {
5+
throw new Error('Invalid day value.')
6+
}
7+
}
8+
9+
function parseDate(dateString) {
10+
const regex = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/
11+
12+
const match = dateString.match(regex)
13+
14+
if (!match) {
15+
throw new Error("Invalid date format. Please use 'dd/mm/yyyy'.")
16+
}
17+
18+
const res = {
19+
day: parseInt(match[1], 10),
20+
month: parseInt(match[2], 10),
21+
year: parseInt(match[3], 10)
22+
}
23+
checkDate(res)
24+
return res
25+
}
26+
27+
export { parseDate }

‎Timing-Functions/test/ParseDate.test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { parseDate } from '../ParseDate'
2+
3+
describe('parseDate', () => {
4+
it.each([
5+
['18/03/2024', { year: 2024, month: 3, day: 18 }],
6+
['29/02/2024', { year: 2024, month: 2, day: 29 }],
7+
['28/02/2023', { year: 2023, month: 2, day: 28 }],
8+
['01/12/2024', { year: 2024, month: 12, day: 1 }],
9+
['1/12/2024', { year: 2024, month: 12, day: 1 }],
10+
['10/1/2024', { year: 2024, month: 1, day: 10 }]
11+
])('Returns correct output for %s', (dateString, expected) => {
12+
expect(parseDate(dateString)).toStrictEqual(expected)
13+
})
14+
15+
it.each([
16+
'18-03-2024',
17+
'18.03.2024',
18+
'03/2024',
19+
'01/02/03/2024',
20+
'123/03/2024'
21+
])('Throws for %s', (wrongDateString) => {
22+
expect(() => {
23+
parseDate(wrongDateString)
24+
}).toThrow()
25+
})
26+
27+
it.each([
28+
'40/03/2024',
29+
'30/02/2024',
30+
'29/02/2023',
31+
'31/04/2023',
32+
'00/01/2024',
33+
'01/00/2024',
34+
'01/13/2024'
35+
])('Throws for %s', (wrongDateString) => {
36+
expect(() => {
37+
parseDate(wrongDateString)
38+
}).toThrow()
39+
})
40+
})

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /