Skip to content

Commit 222dfe4

Browse files
Added the Other algorithm category
1 parent 6e2506b commit 222dfe4

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

Other/DateToDay.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
DateToDay Method
3+
----------------
4+
The DateToDay method takes a date in string format and returns
5+
the name of a day. The approach behind this method is very
6+
simple, we first take a string date and check whether their date
7+
is valid or not, if the date is valid then we pass this date
8+
to the predefined date method and get the date of the day We do
9+
it and find the name of the day, after that give the name of
10+
the day to the user, if the date is wrong, then tell the user
11+
that the given date is not valid.
12+
*/
13+
14+
const monthsNameList = { // create a monthsNameList for easy to use.
15+
1: 'January',
16+
2: 'February',
17+
3: 'March',
18+
4: 'April',
19+
5: 'May',
20+
6: 'June',
21+
7: 'July',
22+
8: 'August',
23+
9: 'September',
24+
10: 'October',
25+
11: 'November',
26+
12: 'December'
27+
}
28+
29+
// show the week day in a number : Sunday - Saturday => 0 - 6
30+
const daysNameList = { // weeks-day
31+
0: 'Sunday',
32+
1: 'Monday',
33+
2: 'Tuesday',
34+
3: 'Wednesday',
35+
4: 'Thursday',
36+
5: 'Friday',
37+
6: 'Saturday'
38+
}
39+
40+
const DateToDay = (date) => {
41+
// firstly, check that input is a string or not.
42+
if (typeof date !== 'string') {
43+
return new TypeError('Argument is not a string.')
44+
}
45+
// extarct the date
46+
const [day, month, year] = date.split('/').map((x) => Number(x))
47+
// check the data are valid or not.
48+
if (day > 31 || month > 12) {
49+
return new TypeError('Date is not valid.')
50+
}
51+
// create a base date for finding the actuale date.
52+
const baseDate = `${monthsNameList[month]} ${day}, ${year} 23:15:30`
53+
// use the Date class and make an object use of the base date.
54+
const finalDate = new Date(baseDate)
55+
// call a getDay() method of Date() class.
56+
const finalDay = finalDate.getDay()
57+
// return the output.
58+
return daysNameList[finalDay]
59+
}
60+
61+
module.exports = DateToDay

0 commit comments

Comments
 (0)